【问题标题】:How to run headless mode in chrome driver using java如何使用 java 在 chrome 驱动程序中运行无头模式
【发布时间】:2017-06-27 05:47:43
【问题描述】:

这是我的 chrome 驱动程序代码:-

String driverPath = driverFile.getAbsolutePath();
                System.setProperty("webdriver.chrome.driver", driverPath);

 Callable<ChromeDriver> startChromedriver = new Callable<ChromeDriver>() {
                    public ChromeDriver call() {
                        ChromeOptions Chromeoptions = new ChromeOptions();
                        Chromeoptions.addArguments("--startMaximized");


                        caps.setCapability("newCommandTimeout", 300);
                        caps.setCapability(ChromeOptions.CAPABILITY, Chromeoptions);

                        return new ChromeDriver(caps);
                    }
                };

我已经在终端启动了以下内容:-

Xvfb -ac :99 -screen 0 1280x1024x16 &

export DISPLAY=:99

然后在 Intellij 中开始我的 Junit 测试

如何在 ubuntu 机器上使用 chrome 驱动在 java 中运行 junit 测试?

对于 Firefox,我已经尝试过了,它的工作原理。

apt-get update

sudo apt-get install xvfb

sudo apt-get install -y xorg xvfb dbus-x11 xfonts-100dpi xfonts-75dpi xfonts-cyrillic


sudo Xvfb :2 -ac


export DISPLAY=:2

Firefox 中的设置

// Setup firefox binary to start in Xvfb
String Xport = System.getProperty(
"lmportal.xvfb.id", ":2");
final File firefoxPath = new File(System.getProperty(
"lmportal.deploy.firefox.path", "/usr/bin/firefox"));
FirefoxBinary firefoxBinary = new FirefoxBinary(firefoxPath);
firefoxBinary.setEnvironmentProperty("DISPLAY", Xport);

// Start Firefox driver
WebDriver driver = new FirefoxDriver(firefoxBinary, null);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://google.com/");

System.out.println("page source" + driver.getCurrentUrl());

【问题讨论】:

    标签: java google-chrome selenium junit headless


    【解决方案1】:

    从 Chrome 59 开始,您甚至不需要 Xvfb。

    在此处下载 Chrome 驱动程序:https://chromedriver.storage.googleapis.com/index.html?path=2.38/

    或者在 macOS 上:

    brew install chromedriver
    

    然后在你的 pom.xml/graddle 中添加一个最新版本的 Selenium:

    <dependency>  
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.8.1</version>
    </dependency> 
    

    当然,您需要 Chrome 版本 > 59

    Java 部分来了:

    String chromeDriverPath = "/Path/To/Chromedriver" ;  
    System.setProperty("webdriver.chrome.driver", chromeDriverPath);  
    ChromeOptions options = new ChromeOptions();  
    options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");  
    WebDriver driver = new ChromeDriver(options); 
    

    我在这里写了一篇详细说明的博客文章: https://ksah.in/introduction-to-chrome-headless/

    【讨论】:

      【解决方案2】:

      使用 redwoodHQ 服务器和 Linux 代理。然后浏览到服务器以驱动代理。

      【讨论】:

      • 有没有办法修改现有的 selenium 代码并在 AWS 实例或本地机器中使用它?
      • @karan 是的,它允许您从 selenium 和 testng 导入代码
      • 我想在 AWS ec2 实例中无头运行我的 junit。我希望它不会帮助我做到这一点。 RedwoodHQ 正在用户界面中打开。
      • @karan RedwoodHq 有一个服务器和一个客户端(称为代理)。在您想要无头驱动的机器上安装代理,并将服务器安装在远程机器上。登录到服务器/远程机器上的 Web 界面,然后将您的 selenium 代码粘贴到测试脚本部分。还有一个用于上传 jar 文件的上下文菜单。 Web 界面将驱动您的客户端计算机。
      • 感谢您的澄清。除了这个还有其他方法吗?
      猜你喜欢
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      相关资源
      最近更新 更多