1、selenium各版本
selenium各版本 http://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
2、确认使用的selenium版本及版本对应的firefox
从selenium 3.0.0开始就要求firefox为48及以上版本,可以通过selenium changelog查看。
changelog查看方式:
(1)打开https://docs.seleniumhq.org/download/,然后 Mozilla GeckoDriver的change log
https://github.com/mozilla/geckodriver/blob/release/CHANGES.md
(2)由于浏览器访问限制无法打开selenium官网的change log, 可以通过百度搜索selenium changelog去Github上查看。
【Selenium】 -> 【FireFox】
2.25.0 -> 18
2.30.0 -> 19
2.31.0 -> 20
2.42.2 -> 29
2.44.0 -> 33 (不支持31)
2.53.0 -> 43,46(不支持47)
2.41.0 -> 26(绿色版本)
2.44 -> 32.0-35.0
2.53.0-2.53.6 -> 40.0.3(本司目前使用selenium和firefox版本)
3、下载火狐浏览器
火狐浏览器各版本 http://ftp.mozilla.org/pub/firefox/releases/
4、下载geckodriver
https://github.com/mozilla/geckodriver
https://github.com/mozilla/geckodriver/releases/
下载时一定要确认geckodriver与firefox的版本对应,如版本v0.21.0下的版本说明
Note that with this release of geckodriver the minimum recommended
Firefox and Selenium versions have changed:
- Firefox 57 (and greater)
- Selenium 3.11 (and greater)
5、程序代码:
public static WebDriver getFirefoxDriver(String url) {
try {
String osys = System.getProperty("os.name").substring(0,1);
String geckodriver = "/usr/bin/geckodriver";
String firefox = "/usr/bin/firefox";
if("w".equalsIgnoreCase(osys)) {
geckodriver = "C:\\Program Files\\Mozilla Firefox\\geckodriver.exe";
firefox = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
}
System.setProperty("webdriver.gecko.driver", geckodriver);
System.setProperty("webdriver.firefox.bin", firefox);
System.setProperty("webdriver.firefox.marionette", firefox);
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setCapability("marionette", true);
capability.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
WebDriver driver = new FirefoxDriver(capability);
//通过driver控制浏览器打开链接(url)
driver.get(url);
// driver.manage().window().maximize();
return driver;
} catch (Exception e) {
System.out.println("oh, no Exception");
e.printStackTrace();
}
return null;
}
6、报错 Error: no DISPLAY environment variable specified
表示没有可供显示的环境,因为是在命令行模式下(无头模式)试图启动firefox,解决方式有两种:
(1)没有Xserver环境。 如果你是ssh远程到服务器,那么你必须开启ssh X forward; 即 ssh 时带 -X 参数, 同时服务器sshd配置开启xforward特性; 如果是本地,那你本地就需要先进GUI环境。有xorg环境后,再执行.
(2)使用无头模式
貌似window/mac 版本需要火狐版本56+,linux版本55+。见 https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode
贴一下java代码吧:
package com.mozilla.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import java.util.concurrent.TimeUnit;
public class HeadlessFirefoxSeleniumExample {
public static void main(String [] args) {
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
System.setProperty("webdriver.gecko.driver", "/opt/geckodriver");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
FirefoxDriver driver = new FirefoxDriver(firefoxOptions);
try {
driver.get("http://www.google.com");
driver.manage().timeouts().implicitlyWait(4,
TimeUnit.SECONDS);
WebElement queryBox = driver.findElement(By.name("q"));
queryBox.sendKeys("headless firefox");
WebElement searchBtn = driver.findElement(By.name("btnK"));
searchBtn.click();
WebElement iresDiv = driver.findElement(By.id("ires"));
iresDiv.findElements(By.tagName("a")).get(0).click();
System.out.println(driver.getPageSource());
} finally {
driver.quit();
}
}
}
从Selenium V3.6开始,Selenium在Options类中提供了一个setHeadless()的方法,通过调用这个方法我们可以轻松实现让脚本运行在Headless模式下,如下:
package learnwebdriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class ChromeHeadless {
public static void main(String[] args) {
// 指定chrome driver的获取地址
System.setProperty("webdriver.chrome.driver", "D:\\BrowserDriver\\chromedriver.exe");
//设置chrome headless模式为true
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
//实例化webdriver的对象,以headless模式启动谷歌浏览器
WebDriver driver = new ChromeDriver(options);
//通过对象driver调用具体的get方法来打开网页
driver.get("http://www.baidu.com/");
//最大化浏览器窗口
driver.manage().window().maximize();
//打印网页标题
System.out.println(driver.getTitle());
//退出浏览器
driver.quit();
}
}
运行程序,会看到谷歌浏览器没有启动,但是控制台不断有日志输出,还打印输出了百度的标题信息,说明运行正常。