selenium 載入jquery

來源:互聯網
上載者:User

標籤:drive   query   ++   ade   out   nts   example   document   open   

 1 packagecom.example.tests; 2  3 import staticorg.junit.Assert.*; 4 importjava.util.*; 5 importorg.junit.*; 6 importorg.openqa.selenium.*; 7 importorg.openqa.selenium.ie.InternetExplorerDriver; 8  9 public classSelenium2{10     WebDriver driver = newInternetExplorerDriver();11     JavascriptExecutor jse = (JavascriptExecutor) driver;12 13     @Test14     public voidjQueryTest() {15         driver.get("http://www.baidu.com/");16         injectjQueryIfNeeded();17         18         List<WebElement> elements = (List<WebElement>) jse.executeScript("return jQuery.find(‘#nv a‘)");19         20         assertEquals(7, elements.size()); // 驗證超連結的數量21         for (inti = 0; i < elements.size(); i++) {22             System.out.print(elements.get(i).getText() + "、");23         }24         25         driver.close();26     }27     28     private voidinjectjQueryIfNeeded() {29         if(!jQueryLoaded())30             injectjQuery();31     }32 33     // 判斷是已載入jQuery34     publicBoolean jQueryLoaded() {35         Boolean loaded;36         try{37             loaded = (Boolean) jse.executeScript("return "+ "jQuery()!=null");38         } catch(WebDriverException e) {39             loaded = false;40         }41         returnloaded;42     }43 44     // 通過注入jQuery45     public voidinjectjQuery() {46         jse.executeScript(" var headID = "47                 + "document.getElementsByTagName(\"head\")[0];"48                 + "var newScript = document.createElement(‘script‘);"49                 + "newScript.type = ‘text/javascript‘;" + "newScript.src = "50                 + "‘http://ajax.googleapis.com/ajax/"51                 + "libs/jquery/1.7.2/jquery.min.js‘;"52                 + "headID.appendChild(newScript);");53     }54 }

 

selenium 載入jquery

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.