gpt4 book ai didi

java - Selenium webdriver 无法在 chrome headless 模式下定位元素

转载 作者:搜寻专家 更新时间:2023-11-01 00:58:37 24 4
gpt4 key购买 nike

我编写了以下脚本来练习 Selenium 浏览器自动化。它会访问 Steam 的网站并更改您的 Steam 用户名。如果在没有 Chrome headless 模式的情况下运行,它可以完美运行,但如果以 options.addArguments("headless") 启动,则无法找到第一个元素。代码:

@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();
}

intellij 崩溃时的打印输出:

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)

这令人困惑,因为以下代码可以完美运行(从 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();
}

那么我在这里缺少什么?我看不出两者之间有任何严重的区别。是什么阻止我的脚本在 headless 模式下定位页面元素?

最佳答案

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

关于java - Selenium webdriver 无法在 chrome headless 模式下定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com