不使用 xvfb 运行无头 Selenium

发布于 2024-08-16 07:33:18 字数 125 浏览 7 评论 0原文

我正在尝试无头运行 Selenium(不显示浏览器)。其他问题指出 xvfb 作为执行此操作的工具。然而,它似乎非常不稳定,总是崩溃,所以我正在寻找另一种替代方案。

有没有非 xvfb 方式来无头运行 Selenium?

I'm trying to run Selenium headless (without the browser appearing). Other questions have pointed to xvfb as the tool to do this. However, it appears highly unstable, crashing all the time, so I'm looking for another alternative.

Is there a non-xvfb way of running Selenium headless?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囍笑 2024-08-23 07:33:18

我认为如果不运行 X 服务器,您将无法运行浏览器。

如果您不喜欢 Xvfb,那么正如 Pascal 所说,您最好的选择可能是运行 VNC 服务器 - 我个人喜欢 Xtightvnc。这意味着您正在运行一个(无头)X 服务器,您可以随时通过 VNC 进入该服务器,以防出现问题并且您想查看它。我始终运行一个 VNC 服务器,并且使用指向该服务器的 $DISPLAY 环境变量来运行测试。

(有人对我投了反对票,所以也许我应该澄清一下:像 Xtightvnc 这样的 X11 VNC 服务器与 Windows 或 OS X 上的常用 VNC 服务器不同,后者只是在网络上共享您现有的屏幕。不要混淆。;-))

I don't think you'll be able to run a browser without running an X server.

If you don't like Xvfb, then as Pascal said, your best bet might be to run a VNC server -- I personally like Xtightvnc. This means you're running a (headless) X server that you can VNC into at any time, in case things go wrong and you want to look at it. I always have a VNC server running, and I'm running my tests with the $DISPLAY environment variable pointing to that server.

(Someone's downvoted me, so maybe I should clarify: X11 VNC servers like Xtightvnc are not the same as the usual VNC servers on Windows or OS X, which would simply share your existing screen on the network. Do not confuse. ;-) )

薄情伤 2024-08-23 07:33:18

我很惊讶。我已经使用过 Selenium 和 Xvfb 多次,没有任何问题,许多其他用户也在这样做。您能否更具体地说明您的设置以及您面临的问题?如何启动 Xvfb?您能提供xvfb.log吗?

但是,为了回答您的问题,可以使用 X VNC 服务器。例如,请参阅此页面了解一些说明。如果没有任何有关配置的详细信息,实际上很难更精确。

I'm surprised. I've used Selenium and Xvfb several times without any problems and many others users are doing so too. Can you be more specific about your setup and the problems you are facing? How do you start Xvfb? Can you provide xvfb.log?

However, to answer your question, it is possible to use an X VNC server. See for example this page for some instructions. It's actually hard to be more precise without any details about your configuration.

明媚殇 2024-08-23 07:33:18

使用 --headless 运行 chrome 浏览器,还可以减少资源使用。使用
ChromeOptions.addArguments("--headless", "window-size=1024,768", "--no-sandbox")
来实现它。该方案假设安装了Chrome浏览器和Chromedriver。

这是我在 Jenkins 工作中使用的简单 Selenium java 测试

    package com.gmail.email;

import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class FirstTest {
    private static ChromeDriver driver;
    WebElement element;

    @BeforeClass
    public static void openBrowser(){

        ChromeOptions ChromeOptions = new ChromeOptions();
        ChromeOptions.addArguments("--headless", "window-size=1024,768", "--no-sandbox");
        driver = new ChromeDriver(ChromeOptions);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test // Marking this method as part of the test
    public void gotoHelloWorldPage() {
        // Go to the Hello World home page
        driver.get("http://webapp:8080/helloworld/");

        // Get text from heading of the Hello World page
        String header = driver.findElement(By.tagName("h2")).getText();
        // Verify that header equals "Hello World!"
        Assert.assertEquals(header, "Hello World!");

    }

    @AfterClass
    public static void closeBrowser(){
        driver.quit();
    }
}

更多详细信息请参见此处
https://github.com/SeleniumHQ/docker-selenium/issues/429

Run chrome browser with --headless, also it allows you to reduce resource usage.Use
ChromeOptions.addArguments("--headless", "window-size=1024,768", "--no-sandbox")
to achieve it. This scheme assumes installed the Chrome browser and Chromedriver.

Here is my simple Selenium java test that is use in my Jenkins job

    package com.gmail.email;

import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class FirstTest {
    private static ChromeDriver driver;
    WebElement element;

    @BeforeClass
    public static void openBrowser(){

        ChromeOptions ChromeOptions = new ChromeOptions();
        ChromeOptions.addArguments("--headless", "window-size=1024,768", "--no-sandbox");
        driver = new ChromeDriver(ChromeOptions);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test // Marking this method as part of the test
    public void gotoHelloWorldPage() {
        // Go to the Hello World home page
        driver.get("http://webapp:8080/helloworld/");

        // Get text from heading of the Hello World page
        String header = driver.findElement(By.tagName("h2")).getText();
        // Verify that header equals "Hello World!"
        Assert.assertEquals(header, "Hello World!");

    }

    @AfterClass
    public static void closeBrowser(){
        driver.quit();
    }
}

More details here
https://github.com/SeleniumHQ/docker-selenium/issues/429

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文