【发布时间】: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