[WebDriver]等待页面加载,(转载出自jarvi)

  1. Wait.html<wbr><wbr></wbr></wbr>
  2. <wbr><wbr></wbr></wbr>
  3. <html><wbr><wbr></wbr></wbr>
  4. <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>head</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  5. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>title</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span>Set<wbr>Timeout<span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;/</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>title</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  6. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>style</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  7. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.red_box<wbr>{background-color:<wbr>red;<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:red">width</span><wbr>=<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">20</span>%;<wbr>height:<wbr>100px;<wbr>border:<wbr>none;}<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  8. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;/</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>style</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  9. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>script</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  10. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>function<wbr>show_div(){<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  11. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>setTimeout("create_div()",<wbr>5000);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  12. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  13. <wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
  14. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>function<wbr>create_div(){<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  15. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:red">d</span><wbr>=<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">document</span>.createElement_x_x('div');<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  16. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:red">d.className</span><wbr>=<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"red_box"</span>;<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  17. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>document.body.a(d);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  18. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  19. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;/</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>script</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  20. <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;/</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>head</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  21. <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>body</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  22. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>button</strong></span><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:red">id</span><wbr>=<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"b"</span><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:red">onclick</span><wbr>=<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"show_div()"</span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span>click<span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;/</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>button</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  23. <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&lt;/</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>body</strong></span><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,102,153)"><strong>&gt;</strong></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  24. </html><wbr><wbr></wbr></wbr>

<wbr></wbr>

下面的代码实现了高亮动态生成的div块的功能:

<wbr></wbr>

Java代码<wbr>(){ @Override public WebElement apply(WebDriver d) { return d.findElement(By.id("b")); }}).click(); WebElement element = dr.findElement(By.cssSelector(".red_box")); ((JavascriptExecutor)dr).executeScript("arguments[0].style.border = \"5px solid yellow\"",element); } }" quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"&gt;<wbr><a href="http://jarvi.iteye.com/blog/1453662" title="收藏这段代码" style="text-decoration:none; color:rgb(62,115,160)"><img src="http://jarvi.iteye.com/images/icon_star.png" alt="收藏代码" title="[WebDriver]等待页面加载,(转载出自jarvi)" style="margin:0px; padding:0px; border:0px; list-style:none"></a></wbr></wbr>
  1. import<wbr>org.openqa.selenium.By;<wbr><wbr></wbr></wbr></wbr>
  2. import<wbr>org.openqa.selenium.JavascriptExecutor;<wbr><wbr></wbr></wbr></wbr>
  3. import<wbr>org.openqa.selenium.WebDriver;<wbr><wbr></wbr></wbr></wbr>
  4. import<wbr>org.openqa.selenium.WebElement;<wbr><wbr></wbr></wbr></wbr>
  5. import<wbr>org.openqa.selenium.firefox.FirefoxDriver;<wbr><wbr></wbr></wbr></wbr>
  6. import<wbr>org.openqa.selenium.support.ui.ExpectedCondition;<wbr><wbr></wbr></wbr></wbr>
  7. import<wbr>org.openqa.selenium.support.ui.WebDriverWait;<wbr><wbr></wbr></wbr></wbr>
  8. <wbr><wbr></wbr></wbr>
  9. <wbr><wbr></wbr></wbr>
  10. public<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>class</strong></span><wbr>WaitForSomthing<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  11. <wbr><wbr></wbr></wbr>
  12. <wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  13. <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>public</strong></span><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>static</strong></span><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>void</strong></span><wbr>main(String[]<wbr>args)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  14. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,130,0)">//<wbr>TODO<wbr>Auto-generated<wbr>method<wbr>stub</wbr></wbr></wbr></wbr></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  15. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.setProperty(<span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"webdriver.firefox.bin"</span>,<span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"D:\\Program<wbr>Files\\Mozilla<wbr>Firefox\\firefox.exe"</wbr></wbr></span>);<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  16. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>WebDriver<wbr>dr<wbr>=<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>new</strong></span><wbr>FirefoxDriver();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  17. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>String<wbr>url<wbr>=<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"file:///C:/Documents<wbr>and<wbr>Settings/gongjf/桌面/selenium_test/Wait.html"</wbr></wbr></span>;//<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"/Your/Path/to/Wait.html"</span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  18. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>dr.get(url);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  19. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>WebDriverWait<wbr>wait<wbr>=<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>new</strong></span><wbr>WebDriverWait(dr,<span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(192,0,0)">10</span>);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  20. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>wait.until(<span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>new</strong></span><wbr>ExpectedCondition&lt;WebElement&gt;(){<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  21. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(100,100,100)">@Override</span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  22. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>public</strong></span><wbr>WebElement<wbr>apply(WebDriver<wbr>d)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  23. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>return</strong></span><wbr>d.findElement(By.id(<span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"b"</span>));<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  24. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}}).click();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  25. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  26. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>WebElement<wbr>element<wbr>=<wbr>dr.findElement(By.cssSelector(<span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">".red_box"</span>));<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  27. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>((JavascriptExecutor)dr).executeScript(<span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"arguments[0].style.border<wbr>=<wbr>\"5px<wbr>solid<wbr>yellow\""</wbr></wbr></wbr></wbr></span>,element);<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  28. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  29. <wbr><wbr><wbr><wbr>}<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  30. }<wbr><wbr></wbr></wbr>

<wbr></wbr>

上面的代码WebDriverWait类的构造方法接受了一个WebDriver对象和一个等待最长时间(10秒)。然后调用until方法,其中重写了ExpectedCondition接口中的apply方法,让其返回一个WebElement,即加载完成的元素,然后点击。默认情况下,WebDriverWait每500毫秒调用一次ExpectedCondition,直到有成功的返回,当然如果超过设定的值还没有成功的返回,将抛出异常。

<wbr></wbr>

隐性等待

<wbr></wbr>

隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉WebDriver查询Dom一定时间。默认值是0,但是设置之后,这个时间将在WebDriver对象实例整个生命周期都起作用。上面的代码就变成了这样:

<wbr></wbr>

Java代码<wbr><wbr><a href="http://jarvi.iteye.com/blog/1453662" title="收藏这段代码" style="text-decoration:none; color:rgb(62,115,160)"><img src="http://jarvi.iteye.com/images/icon_star.png" alt="收藏代码" title="[WebDriver]等待页面加载,(转载出自jarvi)" style="margin:0px; padding:0px; border:0px; list-style:none"></a></wbr></wbr>
  1. import<wbr>java.util.concurrent.TimeUnit;<wbr><wbr></wbr></wbr></wbr>
  2. <wbr><wbr></wbr></wbr>
  3. import<wbr>org.openqa.selenium.By;<wbr><wbr></wbr></wbr></wbr>
  4. import<wbr>org.openqa.selenium.JavascriptExecutor;<wbr><wbr></wbr></wbr></wbr>
  5. import<wbr>org.openqa.selenium.WebDriver;<wbr><wbr></wbr></wbr></wbr>
  6. import<wbr>org.openqa.selenium.WebElement;<wbr><wbr></wbr></wbr></wbr>
  7. import<wbr>org.openqa.selenium.firefox.FirefoxDriver;<wbr><wbr></wbr></wbr></wbr>
  8. import<wbr>org.openqa.selenium.support.ui.ExpectedCondition;<wbr><wbr></wbr></wbr></wbr>
  9. import<wbr>org.openqa.selenium.support.ui.WebDriverWait;<wbr><wbr></wbr></wbr></wbr>
  10. <wbr><wbr></wbr></wbr>
  11. <wbr><wbr></wbr></wbr>
  12. public<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>class</strong></span><wbr>WaitForSomthing<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  13. <wbr><wbr></wbr></wbr>
  14. <wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  15. <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>public</strong></span><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>static</strong></span><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>void</strong></span><wbr>main(String[]<wbr>args)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  16. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,130,0)">//<wbr>TODO<wbr>Auto-generated<wbr>method<wbr>stub</wbr></wbr></wbr></wbr></span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  17. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>System.setProperty(<span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"webdriver.firefox.bin"</span>,<span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"D:\\Program<wbr>Files\\Mozilla<wbr>Firefox\\firefox.exe"</wbr></wbr></span>);<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  18. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>WebDriver<wbr>dr<wbr>=<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(127,0,85)"><strong>new</strong></span><wbr>FirefoxDriver();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  19. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  20. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,130,0)">//设置10秒</span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  21. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>dr.manage().timeouts().implicitlyWait(<span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(192,0,0)">10</span>,<wbr>TimeUnit.SECONDS);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  22. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  23. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>String<wbr>url<wbr>=<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"file:///C:/Documents<wbr>and<wbr>Settings/gongjf/桌面/selenium_test/Wait.html"</wbr></wbr></span>;//<wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"/Your/Path/to/Wait.html"</span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  24. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>dr.get(url);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  25. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; line-height:18px; color:rgb(0,130,0)">//注释掉原来的</span><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  26. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  27. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>dr.findElement(By.id(<span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"b"</span>)).click();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  28. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>WebElement<wbr>element<wbr>=<wbr>dr.findElement(By.cssSelector(<span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">".red_box"</span>));<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  29. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>((JavascriptExecutor)dr).executeScript(<span style="word-wrap:normal; word-break:normal; line-height:18px; color:blue">"arguments[0].style.border<wbr>=<wbr>\"5px<wbr>solid<wbr>yellow\""</wbr></wbr></wbr></wbr></span>,element);<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  30. <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  31. <wbr><wbr><wbr><wbr>}<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  32. }<wbr><wbr></wbr></wbr>

<wbr></wbr>

两者选其一,第二种看起来一劳永逸呀。哈哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值