硒和无头环境。
我们可以在无头环境中执行 Selenium。无头执行是当今行业遵循的新趋势,因为它速度快且支持不止一种浏览器。
一旦配置了 geckodriver 路径,就可以在无头模式下运行 Firefox。然后,我们将使用 FirefoxOptions 类,并通过 setHeadless 方法将无头知识发送给浏览器,并将 true 作为参数传递给它。
语法
FirefoxOptions o = new FirefoxOptions(); o.setHeadless(true); WebDriver driver = new FirefoxDriver(o);
实例
代码实现。
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import java.util.concurrent.TimeUnit;
public class HeadlessFirefox{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
//FirefoxOptions object creation
FirefoxOptions o = new FirefoxOptions();
//set true to headless mode
o.setHeadless(true);
// add options parameter to Firefox driver
WebDriver driver = new FirefoxDriver(o);
// wait of 5 seconds
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://tutorialspoint.com/questions/index.php");
// get page title
System.out.println("Page Title in headless mode: " + driver.getTitle());
}
}输出

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP