在自动化测试或网络爬虫运行时,经常会用到网络状况不好的情况,那么之前写的代码就经常会出现异常,比如 NoSuchElementException或者 TimoutException 之类的。如下错误代码在网络不佳的情况下就会报错:
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; public class NewTest{ public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" , "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); WebDriver driver = new ChromeDriver(); try{ driver.get("http://shanghai.anjuke.com"); WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']")); input.sendKeys("selenium"); }catch(Exception e){ e.printStackTrace(); }finally{ driver.quit(); } }
如果用 WebDriverWait这个类即可对代码进行可控。
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedCondition; public class NewTest{ public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" , "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); WebDriver driver = new ChromeDriver(); try{ //设置页面加载超时时间为3S driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS); driver.get("http://shanghai.anjuke.com"); }catch(Exception e){ //3秒后抛出异常,但程序会继续向下处理,页面也会继续加载 }finally{ //最多等待10S,每2S检查一次 WebDriverWait wait=new WebDriverWait(driver,10,2000); wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { System.out.println("sleep"); return !(driver.findElements(By.xpath("//div[@id='city-panel']")).size() > 0); } }); driver.quit(); } }
如果 你觉得这段代码不好用,也可以自己写。如下:
//每2S 检查一次,但无休止地检查,直到页面加载完成为止 while(true){ if( !(driver.findElements(By.xpath("//div[@id='city-panel']")).size() > 0)) { Thread.sleep(2000); }else { break; } }
相关推荐
Building a flexible and extensible framework around Selenium.pdf 博文链接:https://numenzq.iteye.com/blog/187408
录制工具selenium_ide_button-1.2.0-fx.xpi
selenium处理模式对话框 selenium处理模式对话框 selenium处理模式对话框 selenium处理模式对话框 selenium处理模式对话框
selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium
这是基于python所编写的网络爬虫,用到了python中的selenium库
对于HTTPS安全性弹出窗口证书的处理,见Selenium RC。 对非网页弹出窗口,如window.alert,window.confirm,window.prompt,window. showModalDialog等,有如下方法: 1.封装Windows Api,对Java语言则有Java ...
)控件操作后,会有alert窗口弹出,在selenium里面没有效果??这 个主要是alert窗口是JavaScript触发生成的,但是属于系统级别的窗口,所以,当Alert窗口弹出以后JavaScript就无法再次操作 了,所以在selenium中...
selenium selenium selenium
Selenium_v2.5 Selenium_v2.5 Selenium_v2.5 Selenium_v2.5Selenium_v2.5 Selenium_v2.5
selenium RC与selenium webdriver的区别
Selenium
selenium
Selenium目前没有提供对IE模态对话框(即通过showModalDialog方法打开的弹出对话框)的处理。原因在于,模态对话框会将父页面的 JS挂起,直至对话框处理完毕才会继续执行父页面JS。因为Selenium的底层实现是基于JS的,...
Selenium是ThoughtWorks公司开发的一套基于WEB应用的测试工具,直接运行在浏览器中,模拟用户的操作,主要包括三个部分selenium-IDE,selenium-core,selenium-rc 。它可以被用于单元测试,回归测试,冒烟测试,集成...
从基础到精通的学习 selenium+webdriver的学习方法
由于Selenium Server在启动浏览器时做了手脚,所以Selenium Server会接收到所有由它启动的浏览器发送的请求。 (6).Selenium Server接收到浏览器的发送的Http请求后,自己重组Http请求,获取对应的Web页面。 (7)....
Selenium IDE 插件
Selenium 官方中文文档