码迷,mamicode.com
首页 > 其他好文 > 详细

selenium学习:操作篇

时间:2015-06-10 01:04:12      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

操作过程中会发现原理虽然还算是简单,问题真是不少呀~~

1. 使用Firefox

firefox自身集成了DriverServer,所以使用firefox不需要下载DriverServer拷贝到firefox安装目录下;

其他浏览器均需要;

步骤:1)代码驱动firefox浏览器

DesiredCapabilities capability=DesiredCapabilities.firefox();  
capability.setCapability("firefox_binary",  "D:\\01 Program Software\\firefox\\firefox.exe");  
WebDriver driver = new FirefoxDriver(capability);

 capability是为了指定firefox的安装地址;selenium只读取它默认的firefox安装地址,如果你的firefox安装的和它认识的不一致,就启动不起来;指定firefox的安装地址的方法有很多种;

PS:selenium和firefox的版本对应是我遇到的一个很恶心的问题;我的firefox是38.0.1版本,使用selenium27~selenium46基本都失败

1. 报错:Exception in thread "main" org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms,这是因为selenium在启动firefox时,先锁住一个7054端口,然后使用7055进行连接;因为7054端口已经锁住了,所以报错,解决办法:关闭打开的7054端口,可以采用杀死进行的办法;

2. 报错:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:据说安装firefox和selenium都是最新版本可能能解决这个问题;

ps:firefox下载地址:http://www.firefox.com.cn/download/,如果调用成功了,注意关闭firefox的字段更新功能;

2. 使用IE

步骤:

1)下载一个系统兼容的IEDriver,eg, IEDriverServer_Win32_2.31.0.zip
2)解压,放在IE的安装目录下;
3)修改代码,System.setProperty("webdriver.ie.driver","C:\\Program Files\\Internet Explorer\\IEDriverServer.exe");

出现过的问题:

1. 启动IE,出现

Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information)

原因是:IE的保护模式不一致;

解决办法:

Internet选项--安全:把所有的启动保护模式都勾上;

参考:http://www.dataguru.cn/thread-477007-1-1.html

2. 网址中获取不到元素

Unable to find element with id == fm-login-id (WARNING: The server did not provide any stacktrace information)

3. 使用chrome

步骤:

1)下载一个系统兼容的chromeDriver
2)解压,放在IE的安装目录下;
3)修改代码,System.setProperty("webdriver.chrome.driver", "C:\\Documents and Settings\\Administrator\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chromedriver.exe");  

 

selenium学习:操作篇

标签:

原文地址:http://www.cnblogs.com/echolxq/p/4564903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!