星空网 > 软件开发 > Java

Selenium WebDriver的使用(一)

Selenium WebDriver的相关介绍及资源下载:http://docs.seleniumhq.org/

在2016年2月份Selenium WebDriver更新到了2.52.0版本,建议在使用旧版本的更新至该版本,在稳定性和性能上提高不少。

Selenium支持驱动众多的浏览器,包括PC、移动端及PhantomJS等。

以在windows下的JAVA开发环境为例,下载http://selenium-release.storage.googleapis.com/2.52/selenium-java-2.52.0.zip,解压后添加到工程中。PC端建议使用Google Chrome和PhantomJS。PhantomJS可以理解为没有界面的Safari(QtWebkit作为渲染引擎,JS引擎是JavascriptCore)。要使Selenium能驱动Google Chrome,还需要根据你的系统环境下载chromedriver,下载地址:https://sites.google.com/a/chromium.org/chromedriver/downloads。在本文撰写的时候,chromedriver最新版本为2.21。需要注意的是,2.21版本支持Chrome的版本为v46-50,如果chromedriver和你使用的Chome版本不匹配,程序可能无法正常工作。

chromedriver下载解压后,可以放在任意目录,在程序中指定好环境变量路径即可:

1 System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\driver\\chromedriver.exe");2 WebDriver webDriver = new ChromeDriver();3 webDriver.get("https://www.google.com/");

在驱动并启动Google Chrome时,浏览器实例是不带有任何用户环境配置的,需要自己加载,举个例子:

指定Google Chrome的缓存目录:

1 ChromeOptions options = new ChromeOptions();2 options.addArguments("--disk-cache-dir="+System.getProperty("user.dir")+"\\cache");3 WebDriver webDriver = new ChromeDriver(options);

command line flag如:

--start-maximized:启动时最大化浏览器窗口;

--window-position=x,y:启动时指定浏览器屏幕坐标;

--window-size=w,h:启动时指定浏览器宽高;

--disk-cache-size=s:指定浏览器可使用的磁盘缓存大小;

--media-cache-size=1:指定媒体文件缓存大小;

--ignore-certificate-errors:忽略证书错误;

--disable-extensions:禁用扩展;

--disable-translate:禁用翻译转换;

更多的可参考:http://www.chromium.org/developers/how-tos/run-chromium-with-flags,http://peter.sh/experiments/chromium-command-line-switches/

另外一种方法是使用capabilities,参考:https://sites.google.com/a/chromium.org/chromedriver/capabilities

如果使用phantomjs,同样的,指定好系统环境变量路径即可:

1 System.setProperty("phantomjs.binary.path", System.getProperty("user.dir") + "\\driver\\phantomjs.exe");2 webDriver = new PhantomJSDriver();3 webDriver.get("https://www.google.com/");

在get()完成,接下来就可以操作浏览器、提取页面内容了,比如:

提取页面中的image:

1 WebElement webElement;2 List<WebElement> webElements;3 webElements = webDriver.findElements(By.cssSelector("body img"));4 for (webElement : webElements) {5   System.out.println(webElement.getAttribute("src"));6   System.out.println(webElement.getSize());7 };

Selenium WebDriver的API参考地址:http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html

如果在get()的时候打开了多个浏览器窗口,可通过switchTo().window(winHandle),切换到各窗口:

 1 String defaultWinHandle = webDriver.getWindowHandle(); 2 if (webDriver.getWindowHandles().size()>1) { 3   for(String winHandle : webDriver.getWindowHandles()){ 4     if (!winHandle.equals(defaultWinHandle)) { 5       webDriver.switchTo().window(winHandle); 6       //do something... 7       //close window: webDriver.close(); 8     }; 9   };10   webDriver.switchTo().window(defaultWinHandle);11 };

在操作完成后,退出:

1 webDriver.quit();

如果页面中有iframe,如何可靠的切入切出?如果iframe还有嵌套怎么办?

待续!

原创文章,转载请标明出处。




原标题:Selenium WebDriver的使用(一)

关键词:web

web
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

美国fba海运价格:https://www.goluckyvip.com/tag/38002.html
美国fba海运价钱:https://www.goluckyvip.com/tag/38003.html
美国fba海运拼箱:https://www.goluckyvip.com/tag/38004.html
美国fba海运渠道:https://www.goluckyvip.com/tag/38005.html
美国fba海运散货拼箱:https://www.goluckyvip.com/tag/38006.html
美国fba海运时效:https://www.goluckyvip.com/tag/38007.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流