Selenium测试无头镀铬的执行情况

时间:2018-04-17 11:04:33

标签: selenium google-chrome-headless

我目前正在无头镀铬浏览器上运行Selenium测试。以下是 我的chrome设置:

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
options.addArguments("--allow-insecure-localhost");
driver = new ChromeDriver(options);

当我尝试阅读一个网络元素时,它说没有这样的元素发现异常。 我也尝试添加以下等待:

    WebDriverWait wait = new WebDriverWait(driver, 150);
    WebElement usernameTextfield = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("j_username")));

这次它说了一个不同的错误:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.name: j_username (tried for 150 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:82)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
at com.bcs.p3s.automationtest.TestP3sLogin.loginCorrectCredentials(TestP3sLogin.java:135)
at com.bcs.p3s.automationtest.TestP3sLogin.main(TestP3sLogin.java:125)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:756)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.name: j_username
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.10.0', revision: '176b4a9', time: '2018-03-02T19:03:16.397Z'
System info: host: 'AVID-OX790-013', ip: '192.168.200.89', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_102'
Driver info: driver.version: unknown
at   org.openqa.selenium.support.ui.ExpectedConditions.lambda$findElement$0(ExpectedConditions.java:896)
at java.util.Optional.orElseThrow(Unknown Source)
at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:895)
at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:44)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:206)
at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:202)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
... 27 more

这个问题只存在于无头铬合金中。当我在实际的Chrome浏览器上运行测试时,它可以工作。 有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:-1)

将此代码用于chrome无头实例:

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME ,true);
driver.get("your web URL");  
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("ppLoginForm"))));
wait.until(ExpectedConditions.elementToBeClickable(By.name("j_username")));

试试这段代码!

答案 1 :(得分:-1)

我要做的是使用脏黑客(此代码在C#中,但与Java非常相似):

while (2 == 2) 
{
  try
 { 
     Thread.sleep(50);
     var element = driver.findElements(By.ClassName("username")); 
     if(element.Count > 0)  //you can change it to .size() for Java
     {
       //do the code here
       break;
     }
 }
  catch(Exception)
 {
 }

}