qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          Selenium web driver 使用JS修改input屬性

          selenium獲取input時候,發(fā)現(xiàn)type=”hidden” 的input無法修改value,經(jīng)牛人指點,可以使用js修改
            首先html源文件如下,設(shè)置為text 、hidden、submit
            View Code
            在瀏覽器加載之后如下:
            這時候email 不能對外顯示
            使用selenium,代碼如下
          1 import org.openqa.selenium.Alert;
          2 import org.openqa.selenium.JavascriptExecutor;
          3 import org.openqa.selenium.By;
          4 import org.openqa.selenium.WebDriver;
          5 import org.openqa.selenium.WebElement;
          6 import org.openqa.selenium.chrome.ChromeDriver;
          7 //import org.openqa.selenium.ie.InternetExplorerDriver;
          8 //import org.openqa.selenium.remote.DesiredCapabilities;
          9
          10 public class selenium {
          11
          12     /**
          13      * @param args
          14      * @throws InterruptedException
          15      */
          16     public static void main(String[] args) throws InterruptedException {
          17         // TODO Auto-generated method stub
          18
          19         String URL="E:\\2.html";
          20         //set web driver property
          21         System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
          22         //create a WebDriver instance
          23         WebDriver driver = new ChromeDriver()    ;
          24         driver.manage().window().maximize();
          25
          26         //load the URL
          27         driver.get(URL);
          28         //print current title
          29         System.out.println(driver.getTitle());
          30         //run JS to modify hidden element
          31         ((JavascriptExecutor)driver).executeScript("document.getElementById(\"em\").type ='text';");
          32         Thread.sleep(3000);
          33         //run JS and add a alert
          34         ((JavascriptExecutor)driver).executeScript("alert(\"hello,this is a alert!\");value=\"Alert\"");
          35
          36         //wait for 3 seconds
          37          Thread.sleep(3000);
          38
          39
          40           //create a alert instance
          41           Alert alert1=driver.switchTo().alert();
          42           //print alert text
          43           System.out.println(alert1.getText());
          44           //click accept button
          45           alert1.accept();
          46
          47             //create elements
          48             WebElement we=driver.findElement(By.id("fn"));
          49             WebElement su=driver.findElement(By.id("su"));
          50             WebElement em=driver.findElement(By.id("em"));
          51             // input something
          52             we.sendKeys("username test");
          53             Thread.sleep(3000);
          54             //print email tagname
          55             System.out.print("Email  isDislayed="+em.isDisplayed()+"\n");
          56             Thread.sleep(3000);
          57             //click submit button
          58             su.click();
          59             Thread.sleep(3000);
          60
          61             Alert alert=driver.switchTo().alert();
          62             System.out.print( alert.getText());
          63             alert.accept();
          64
          65             Thread.sleep(3000);
          66
          67             //close web browser
          68             driver.quit();
          69
          70     }
          71
          72 }


           可以通過js修改input的type value,執(zhí)行js只需要export
            import org.openqa.selenium.JavascriptExecutor;
            運行結(jié)果如下:
            Starting ChromeDriver (v2.9.248315) on port 30175
            this is a test
            hello,this is a alert!
            Email  isDislayed=true
            I am an alert box!!

          posted on 2014-03-25 11:25 順其自然EVO 閱讀(1159) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學(xué)習(xí)

          <2014年3月>
          2324252627281
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 庆元县| 古浪县| 姜堰市| 甘谷县| 柳林县| 松江区| 宣城市| 麦盖提县| 宜州市| 蒲城县| 西乌珠穆沁旗| 寿阳县| 肥城市| 宝鸡市| 开阳县| 称多县| 平武县| 陆川县| 义乌市| 泰来县| 呼伦贝尔市| 德惠市| 南投市| 剑川县| 开封县| 古丈县| 互助| 两当县| 漳州市| 陕西省| 文水县| 海阳市| 广平县| 杭州市| 隆回县| 通渭县| 塔城市| 辽阳市| 乐清市| 昌乐县| 衢州市|