java+selenium+firefox/chrome环境搭建
作者:互联网
最近疫情在家隔离,小小研究一下java自动化 selenium基础到应用,俗话说工欲善其事必先利其器~搭建环境 额卡了好久
1.selenium下载地址:
https://www.selenium.dev/downloads/
2.geckodriver下载地址:
https://github.com/mozilla/geckodriver/releases
selenium用户必须是版本号在3.11或者更高版本以上才可以用geckodriver,而且不同版本的geckodriver支持的浏览器版本也是不同的,否则会在使用脚本启动浏览器时报错
下表显示了geckodriver 版本与所需的 Selenium 和 Firefox 版本之间的映射,具体映射关系参照以下链接:
https://firefox-source-docs.mozilla.org/testing/geckodriver/Support.html
3.创建一个工程,导入selenium相关的jar包,这些就都不赘述了(稍微懂点java的,jdk啥的环境都会配置了吧)
4.证明环境搭建是否成功
public class SeleniumOne {
public static void main(String[] args){
WebDriver driver;
System.setProperty("webdriver.gecko.driver","/Users/gaoxin/Downloads/geckodriver");
driver = new FirefoxDriver();
//打开百度网页
driver.get("https://www.baidu.com/");
driver.quit();
}
}
可成功调起firefox并且打开百度界面
遇到一个小小的问题,错误信息如下:
1652511985545 geckodriver INFO Listening on 127.0.0.1:62120
1652511985942 webdriver::server WARN Rejected request with Host header localhost:62120, allowed values are []
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: Invalid Host header localhost:62120
Build info: version: '4.1.4', revision: '535d840ee2'
System info: host: 'gaoxindeMacBook-Pro.local', ip: 'fdd3:887:eb5e:0:18ac:2dae:c1d2:20c1%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.16', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Command: [null, newSession {capabilities=[Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:debuggerAddress: true, moz:firefoxOptions: {}}], desiredCapabilities=Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:debuggerAddress: true, moz:firefoxOptions: {}}}]
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:84)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:62)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:156)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:567)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:248)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:164)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:118)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:106)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:86)
at com.imooc.web.SeleniumOne.main(SeleniumOne.java:20)
原因: 脚本通过127.0.0.1这个ip访问本地geckodriver服务, hosts中未配置 127.0.0.1指向localhost,
所以导致连接不上geckodriver服务
解决办法: 配置本地hosts, 添加:127.0.0.1 localhost
标签:java,firefox,chrome,openqa,selenium,geckodriver,org 来源: https://www.cnblogs.com/gx1029/p/16267940.html