Running Selenium headless without using xvfb

后端 未结 3 1457
野趣味
野趣味 2021-02-03 13:12

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 unstab

3条回答
  •  甜味超标
    2021-02-03 13:36

    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

提交回复
热议问题