简体   繁体   English

Selenium webdriver 无法在 Chrome 无头模式下定位元素

[英]Selenium webdriver is unable to locate element in chrome headless mode

I wrote the following script to practice Selenium browser automation.我编写了以下脚本来练习 Selenium 浏览器自动化。 It accesses Steam's website and changes your Steam username.它会访问 Steam 的网站并更改您的 Steam 用户名。 It works perfectly if run without Chrome's headless mode but fails to locate the very first element if started with options.addArguments("headless") .如果在没有 Chrome 无头模式的情况下运行,它可以完美运行,但如果以options.addArguments("headless")开头,则无法找到第一个元素。 The code:代码:

@Test
public void steamPowered() throws IOException {

    System.setProperty("webdriver.chrome.driver", "C:\\Driver\\chromedriver.exe");

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--user-data-dir=C://Users/Evan/Downloads/Profile8Aug17");
    options.addArguments("headless");
    options.addArguments("window-size=1200x600");

    ChromeDriver driver = new ChromeDriver(options);

    driver.navigate().to("https://store.steampowered.com/");

    WebElement element = (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.presenceOfElementLocated(By.id("account_pulldown")));
    element.click();

    element = (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"account_dropdown\"]/div/a[4]")));
    element.click();

    element = (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.presenceOfElementLocated(By.xpath
            ("/html/body/div[1]/div[7]/div[2]/div[1]/div[1]/div/div/div/div[3]/div[2]/a/span")));
    element.click();

    element = (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.presenceOfElementLocated(By.id("personaName")));
    element.clear();
    element.sendKeys(scramble(USERNAME));

    driver.findElement(By.xpath("//span[text()='Save Changes']")).click();

    driver.quit();
}

} }

The intellij printout when it crashes:崩溃时的智能打印输出:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate 
element: {"method":"id","selector":"account_pulldown"}
  (Session info: headless chrome=60.0.3112.90)
  (Driver info: chromedriver=2.31.488763 
(092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 
x86_64) (WARNING: The server did not provide any stacktrace information)

This is confusing because the following code works perfectly (copy pasted from https://medium.com/@eliasnogueira/running-selenium-tests-with-chrome-headless-5edd624efb92 )这令人困惑,因为以下代码完美运行(从https://medium.com/@eliasnogueira/running-selenium-tests-with-chrome-headless-5edd624efb92复制粘贴)

@Test
public void testExecution() throws IOException {
    System.setProperty("webdriver.chrome.driver", "C:\\Driver\\chromedriver.exe");

    // Add options to Google Chrome. The window-size is important for responsive sites
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--user-data-dir=C://Users/Evan/Downloads/Profile8Aug17");
    options.addArguments("headless");
    options.addArguments("window-size=1200x600");

    WebDriver driver = new ChromeDriver(options);
    driver.get("http://seleniumhq.org");

    // a guarantee that the test was really executed
    assertTrue(driver.findElement(By.id("q")).isDisplayed());

    driver.quit();
}

So what am I missing here?那么我在这里错过了什么? I fail to see any serious difference between the two.我看不出两者之间有任何严重的区别。 What's stopping my script from locating page elements in headless mode?什么阻止我的脚本在无头模式下定位页面元素?

尝试将屏幕尺寸更改为“1920x1080”,有时您会在屏幕尺寸不合适时发现问题。

Can you please try with the belwo settings:你能不能试试 belwo 设置:

options.addArguments("window-size=1400,800");
options.addArguments("headless");

In order to use headless chrome using ChromeOptions in java you are supposed to use --headless , you may just replace:为了在 Java 中使用 ChromeOptions 使用 headless chrome,您应该使用--headless ,您可以替换:

options.addArguments("headless");

with:与:

options.addArguments("--headless");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM