一.资源准备
1.selenium 下载http://selenium-release.storage.googleapis.com/2.5/selenium-java-2.53.1.zip
下载完成后解压,并将以下两个文件导入到你的项目,jdk7对应selenium2.xx,jdk8对应selenium3.xx
2.chromedriver下载http://chromedriver.storage.googleapis.com/index.html
注:注意同chrome的版本对应问题,不对应将打不开浏览器,版本映射关系请问度娘
3.selenium-server-standalone 下载http://selenium-release.storage.googleapis.com/index.html
二.hub端注册
在本机上进行注册
java -jar selenium-server-standalone-2.53.1.jar -role hub + -port xxxx
默认端口号是4444,端口可自行设定
三.node端注册
在远程机器上注册,首先将上述chromedriver和selenium-server-standalone-2.53.1.jar拷到远程机器
java -jar C:\Users\Administrator\Desktop\selenium-server-standalone-2.53.1.jar -role node -hub http://xxxx:4444/grid/register -Dwebdriver.chrome.driver="C:\Users\Administrator\Desktop\chromedriver.exe" -port 6657 -browser browserName="chrome"
四.直接上代码了
public static void main(String[] args) {
/*System.setProperty("webdriver.chrome.driver","C:\\Users\\xxx\\Desktop\\chromedriver.exe");//chromedriver服务地址
WebDriver driver =new ChromeDriver(); //新建一个WebDriver 的对象,但是new 的是FirefoxDriver的驱动
driver.get("http://wms.cainiao.com/console/portal.htm");//打开指定的网站
try {
*//**
* WebDriver自带了一个智能等待的方法。
dr.manage().timeouts().implicitlyWait(arg0, arg1);
Arg0:等待的时间长度,int 类型 ;
Arg1:等待时间的单位 TimeUnit.SECONDS 一般用秒作为单位。
*//*
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}
*//**
* dr.quit()和dr.close()都可以退出浏览器,简单的说一下两者的区别:第一个close,
* 如果打开了多个页面是关不干净的,它只关闭当前的一个页面。第二个quit,
* 是退出了所有Webdriver所有的窗口,退的非常干净,所以推荐使用quit最为一个case退出的方法。
*//*
driver.quit();//退出浏览器*/
try {
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
URL urlInstance = new URL("http://xxxx:6657/wd/hub"); //指定URL
WebDriver driver = new RemoteWebDriver(urlInstance, capabilities); //使用RemoteWebDriver初始化
driver.get("http://wms.cainiao.com/console/portal.htm");//打开指定的网站
LOGGER.info("Firefox 浏览器启动完成!");
} catch (Exception e) {
LOGGER.error("Firefox 浏览器启动失败!");
LOGGER.error("===============>" + e.getMessage().toString());
e.printStackTrace();
}
}