selenium第一个小程序遇到的问题
来源:互联网 发布:centos u盘挂载 编辑:程序博客网 时间:2024/05/29 17:39
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TestHelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
//代码如下,主要是打开百度,然后在搜索框输入glen,点击搜索按钮,关闭浏览器。
//如果火狐浏览器没有默认安装在C盘,需要制定其路径
System.setProperty("webdriver.firefox.bin", "E:/Program Files/Mozilla Firefox/firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
driver.manage().window().maximize();
WebElement txtbox = driver.findElement(By.name("wd"));
txtbox.sendKeys("selenium");
WebElement btn = driver.findElement(By.id("su"));
btn.click();
driver.close();
}
}
如果火狐浏览器没有按照默认安装到C盘,需要制定其路径
System.setProperty("webdriver.firefox.bin", "E:/Program Files/Mozilla Firefox/firefox.exe");
本人是装在.bin", "E:/Program Files/Mozilla Firefox/firefox.exe下面
否则会报异常
:
Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: XP
Build info: version: '2.18.0', revision: '15704', time: '2012-01-27 17:37:17'
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_23'
解决方法:方法1、最简单的重新安装firefox到默认路径。哈哈
方法2、直接用System.setPropert方法设置webdriver.firefox.bin的值,如
Java代码
System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
方法3、 用FirefoxBinary类和public FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile)这个构造方法,直接上代码
Java代码
File pathToFirefoxBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
WebDriver driver = new FirefoxDriver(firefoxbin,null);//这里使用这个构造方法。
应该还可以在环境变量里面设置firefox的路径也可以,有兴趣的可以试一下。
2 如果selenium和Firefox的版本不兼容,运行代码时,会出现下面异常:
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
tem-share
1413076587749 addons.xpi INFO Mapping {972ce4c6-7e08-4474-a285-3208198ce6fd} to E:\Program Files\Mozilla Firefox\browser\extensions\{972ce4c6-7e08-4474-a285-3208198ce6fd}
1413076587750 addons.xpi INFO Mapping fxdriver@googlecode.com to C:\Users\zhangmin\AppData\Local\Temp\anonymous3316082759202275717webdriver-profile\extensions\fxdriver@googlecode.com
1413076587750 addons.xpi DEBUG Ignoring file entry whose name is not a valid add-on ID: C:\Users\zhangmin\AppData\Local\Temp\anonymous3316082759202275717webdriver-profile\extensions\webdriver-staging
1413076587750 addons.xpi DEBUG checkForChanges
1413076587757 addons.xpi DEBUG Directory state JSON differs: cache [] state [{"name":"app-global","addons":{"{972ce4c6-7e08-4474-a285-3208198ce6fd}":{"descriptor":"E:\\Program Files\\Mozilla Firefox\\browser\\extensions\\{972ce4c6-7e08-4474-a285-3208198ce6fd}","mtime":1412224254982,"rdfTime":1411526203000}}},{"name":"app-profile","addons":{"fxdriver@googlecode.com":{"descriptor":"C:\\Users\\zhangmin\\AppData\\Local\\Temp\\anonymous3316082759202275717webdriver-profile\\extensions\\fxdriver@googlecode.com","mtime":1413076587152,"rdfTime":1413076586824}}}]
1413076587763 addons.xpi-utils DEBUG Opening XPI database C:\Users\zhangmin\AppData\Local\Temp\anonymous3316082759202275717webdriver-profile\extensions.json
1413076587766 addons.xpi DEBUG New add-on fxdriver@googlecode.com installed in app-profile
*** Blocklist::_loadBlocklistFromFile: blocklist is disabled
1413076588018 addons.xpi-utils DEBUG Make addon app-profile:fxdriver@googlecode.com visible
1413076588020 DeferredSave.extensions.json DEBUG Save changes
1413076588021 DeferredSave.extensions.json DEBUG Save changes
1413076588022 addons.xpi DEBUG New add-on {972ce4c6-7e08-4474-a285-3208198ce6fd} installed in app-global
1413076588025 addons.xpi-utils DEBUG Make addon app-global:{972ce4c6-7e08-4474-a285-3208198ce6fd} visible
1413076588025 DeferredSave.extensions.json DEBUG Save changes
1413076588025 DeferredSave.extensions.json DEBUG Save changes
1413076588033 addons.xpi DEBUG Updating database with changes to installed add-ons
1413076588033 addons.xpi-utils DEBUG Updating add-on states
1413076588034 addons.xpi-utils DEBUG Writing add-ons list
1413076589104 DeferredSave.extensions.json DEBUG Starting timer
1413076589138 addons.manager DEBUG shutdown
1413076589194 DeferredSave.extensions.json DEBUG Starting write
1413076589195 addons.xpi DEBUG shutdown
1413076589195 addons.xpi-utils DEBUG shutdown
1413076589263 DeferredSave.extensions.json DEBUG Write succeeded
1413076589263 addons.xpi-utils DEBUG XPI Database saved, setting schema version preference to 16
1413076589264 addons.xpi DEBUG Notifying XPI shutdown observers
1413076589266 addons.manager DEBUG Async provider shutdown done
1413076590479 addons.manager DEBUG Loaded provider scope for resource://gre/modules/addons/XPIProvider.jsm: ["XPIProvider"]
1413076590481 addons.manager DEBUG Loaded provider scope for resource://gre/modules/LightweightThemeManager.jsm: ["LightweightThemeManager"]
1413076590483 addons.xpi DEBUG startup
1413076590483 addons.xpi DEBUG Skipping unavailable install location app-system-local
1413076590483 addons.xpi DEBUG Skipping unavailable install location app-system-share
1413076590484 addons.xpi INFO Mapping {972ce4c6-7e08-4474-a285-3208198ce6fd} to E:\Program Files\Mozilla Firefox\browser\extensions\{972ce4c6-7e08-4474-a285-3208198ce6fd}
1413076590485 addons.xpi INFO Mapping fxdriver@googlecode.com to C:\Users\zhangmin\AppData\Local\Temp\anonymous3316082759202275717webdriver-profile\extensions\fxdriver@googlecode.com
1413076590485 addons.xpi DEBUG Ignoring file entry whose name is not a valid add-on ID: C:\Users\zhangmin\AppData\Local\Temp\anonymous3316082759202275717webdriver-profile\extensions\webdriver-staging
1413076590485 addons.xpi DEBUG checkForChanges
1413076590493 addons.xpi DEBUG No changes found
*** Blocklist::_preloadBlocklistFile: blocklist is disabled
JavaScript error: chrome://browser/content/urlbarBindings.xml, line 677: aUrl is undefined
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:106)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:244)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:110)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:190)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:183)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:179)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:92)
at autoTest_selenium.TestHelloWorld.main(TestHelloWorld.java:13)
解决方案:
selenium版本过低,可以升级selenium,
window8装的Firefox是32版本,对应的selenium版本是2.43
其他:
Selenium ->FireFox
2.25.0 -> 18
2.30.0 ->19
2.31.0 ->20
升级selenium jar包
正常启动firefox
- selenium第一个小程序遇到的问题
- 第一个servlet小程序可能遇到的问题
- 第一个DWR程序遇到的问题
- 第一个MapReduce程序遇到的问题
- 第一个小程序---计算器开发中遇到的问题总结
- Python小白第一个小程序出现的问题
- opengl的第一个程序遇到的问题
- Android的第一个程序遇到的问题
- 菜鸟学习Python+Selenium遇到的第一个要崩溃的问题
- 编译《深入解析MFC》第一个程序遇到的问题
- 编写第一个XML-RPC程序遇到的问题总结
- 【android开发入门】第一个android程序遇到的问题
- opencv第一个测试程序遇到的问题及解决
- directx9程序中遇到的第一个问题 error2061 __RPC__out_xcount_part
- 使用Unity发布第一个程序时遇到的问题
- 创建第一个android程序遇到的问题
- 第一个hibernate程序以及遇到的问题总结
- 遇到的第一个问题
- 【Matlab】Matlab编程学习笔记【待续】
- C语言全局变量定义方法
- WebService_HelloWorld_笔记
- Cocos2d-x各项目的功能
- vim-filetype
- selenium第一个小程序遇到的问题
- LeetCode 题解(48): Minimum Path Sum
- MySql入门命令总结
- 【Information Retrieval】文本信息检索
- python Tkinter (1)
- ARM和NEON指令
- Leetcode-Rotate List
- hdu 5007 Post Robot
- UVA - 10718 Bit Mask(贪心+位运算)