【问题标题】:NoSuchElementExeption in Docker but working fine in localhostDocker 中的 NoSuchElementExeption 但在 localhost 中工作正常
【发布时间】:2020-09-29 19:25:40
【问题描述】:

我在 selenium 中编写了一个测试自动化程序,当我在 eclipse 中运行它时它也可以工作。我想在 Docker 中运行它,但我总是面对 Expetion NoSuchElement。我真的希望有人能帮助我。所以这就是我所做的:

final static String websiteUnderTest = "https://test.com";

final static String remoteWebDriverUrl = "http://selenium:4444/wd/hub";

//       Docker Browser setting
        Capabilities chromeCapabilities = new ChromeOptions();
        ((ChromeOptions) chromeCapabilities).addArguments("--headless");
        driver = new RemoteWebDriver(new URL(remoteWebDriverUrl), chromeCapabilities);

    driver.get(websiteUnderTest);
        wait = new WebDriverWait(driver, 30);
        login();

登录:

public static void login() throws InterruptedException {
        // ---Login LogTicks---
        jse6 = (JavascriptExecutor) driver;
        System.out.println("Login User");
        
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("userid")));
    
        WebElement userID = driver.findElement(By.id("userid"));
        jse6.executeScript("window.scrollTo(0,"+userID.getLocation().y+")");
        userID.click();
        userID.sendKeys("PID6D1F");
        driver.findElement(By.id("next-btn")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("password")));
        WebElement password = driver.findElement(By.id("password"));
        password.sendKeys("LogTicksPool-ID");
        driver.findElement(By.id("loginSubmitButton")).click();

        System.out.println("User PID6D1F has login");
    }

我总是明白:

tests_1     | Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.id: userid (tried for 30 second(s) with 500 milliseconds interval)
tests_1     |   at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:113)
tests_1     |   at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:283)
tests_1     |   at SeleniumTests.login(SeleniumTests.java:99)
tests_1     |   at SeleniumTests.main(SeleniumTests.java:49)
tests_1     | Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.id: userid
tests_1     | For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
tests_1     | Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
tests_1     | System info: host: '390560c4caf4', ip: '172.18.0.3', os.name: 'Linux', os.arch: 'amd64', os.version: '4.19.76-linuxkit', java.version: '10.0.2'
tests_1     | Driver info: driver.version: unknown
tests_1     |   at org.openqa.selenium.support.ui.ExpectedConditions.lambda$findElement$0(ExpectedConditions.java:896)
tests_1     |   at java.base/java.util.Optional.orElseThrow(Optional.java:397)
tests_1     |   at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:895)
tests_1     |   at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:44)
tests_1     |   at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:206)
tests_1     |   at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:202)
tests_1     |   at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:260)
tests_1     |   ... 2 more

正如您在示例中看到的那样,我已经有一个服务员了。

我很乐意为您提供任何帮助! 谢谢

【问题讨论】:

  • 我没有 docker 或 selenium 的 exp,但是这个本地路径 "C:\\Users\\calisay\\eclipse-workspace\\LogTicks-Testautomatisierung\\driver\\chromedriver.exe" 不是让它只能在你的机器上运行吗?
  • 是的,我只在 Eclipse 中本地运行该路径时才使用该路径。我的代码的第二部分带有注释“Docker 浏览器设置”是我想在 Docker 中运行它时使用的。
  • 遇到完全相同的问题 - 一切都在 localhost 中有效,但在 Docker 中无效 - 你有没有想过为什么? @sunxca

标签: java docker selenium google-chrome selenium-chromedriver


【解决方案1】:

此错误消息...

tests_1     | Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
tests_1     | System info: host: '390560c4caf4', ip: '172.18.0.3', os.name: 'Linux', os.arch: 'amd64', os.version: '4.19.76-linuxkit', java.version: '10.0.2'
tests_1     | Driver info: driver.version: unknown

...暗示 ChromeDriver 无法与 Browsing ContextChrome 浏览器 会话进行通信。


根据错误跟踪日志,您使用的 java.version: '10.0.2' 仍不受完全支持。可以在Does Selenium v3.141 support Java 13?找到相关讨论

然而,

Build info: version: 'unknown', revision: 'unknown', time: 'unknown'

暗示ChromeDriver 无法识别Selenium 客户端版本。此外,

Driver info: driver.version: unknown

暗示 ChromeDriver 无法识别回来。

所以 Selenium ClientChromeDriverChrome 浏览器

之间存在明显的不匹配

解决方案

确保:

  • JDK 升级到当前级别JDK 8u251
  • Selenium 升级到当前级别 Version 3.141.59
  • ChromeDriver 已更新到当前的ChromeDriver v83.0 级别。
  • Chrome 已更新至当前 Chrome 版本 83.0 级别。 (根据ChromeDriver v83.0 release notes
  • 如果您的基本 Web 客户端 版本太旧,请卸载它并安装最新的 GA 和发布版本的 Web 客户端
  • 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。
  • 如果您的基本 Web 客户端 版本太旧,请卸载它并安装最新的 GA 和发布版本的 Web 客户端
  • 进行系统重启
  • 非root用户身份执行@Test
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例。

【讨论】:

  • 我按照你说的做了一切,但它仍然没有工作......同样的问题:(
猜你喜欢
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-24
相关资源
最近更新 更多