Selenium Webdriver:预期条件失败:等待位于 IAP 无头模式的元素的可见性 Google

Selenium Webdriver: Expected condition failed: waiting for visibility of element located in headless mode for IAP Google

我正在尝试在具有 IAP google 步骤的应用程序上使用无头模式。当无头模式关闭时,它运行无误。当无头模式开启时,selenium 无法定位元素。

我不知道如何解决这个问题。我试了很多chromedriver的配置,还是一样的错误。

public class Hooks {

    ChromeOptions options = new ChromeOptions();
    public static WebDriver driver;

    @Before
    public void setUp() {
        String chromeDriverPath = "C:\Selenium\Drivers\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", chromeDriverPath);
        options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1080");
        //options.addArguments("--start-maximized");
        driver = new ChromeDriver(options);
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
    }

尝试定位元素时出错:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //button[text() = 'Esqueceu seu e-mail?'] (tried for 5 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)

定位元素的步骤:

public BaseClass(WebDriver driver) {
        BaseClass.driver = driver;
        wait = new WebDriverWait(driver, 5);
    }
    public void waitVisibility(By elementBy) {
        wait.until(ExpectedConditions.visibilityOfElementLocated(elementBy));
    }
    public void isDisplayed(By elementBy) {
        waitVisibility(elementBy);
        driver.findElement(elementBy).isDisplayed();
    }
   public void checkElement() {
        isDisplayed(headerLoginGooglePage);
    }

完整的错误堆栈跟踪

Starting ChromeDriver 2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a) on port 39113
Only local connections are allowed.
dez 12, 2019 4:25:32 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMAÇÕES: Detected dialect: OSS
    Dado que o usuário acesse a url do sistema SMAPP Parts and Maintenance          # LoginSteps.queOUsuarioAcesseUrlSistemaSMAPP()
    Entao exibe tela padrão de login por conta Google                               # LoginSteps.exibeTelaPadraoLoginContaGoogle()
      org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //button[text() = 'Esqueceu seu e-mail?'] (tried for 5 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at pageobjects.BaseClass.waitVisibility(BaseClass.java:21)
    at pageobjects.BaseClass.isDisplayed(BaseClass.java:51)
    at pageobjects.MaintenanceTimeLinePages.visualizarElementoTelaGoogle(MaintenanceTimeLinePages.java:109)
    at step_definitions.LoginSteps.exibeTelaPadraoLoginContaGoogle(LoginSteps.java:34)
    at ✽.exibe tela padrão de login por conta Google(src/test/resources/features/Login.feature:7)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[text() = 'Esqueceu seu e-mail?']"}
  (Session info: chrome=78.0.3904.108)
  (Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 10.0.17763 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-AJU29UT', ip: '192.168.12.226', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_221'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.43.600210 (68dcf5eebde371..., userDataDir: C:\Users\ZUPPER\AppData\Loc...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:51880}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 78.0.3904.108, webStorageEnabled: true}
Session ID: b148fc1021de85032649ce461e775ab2
*** Element info: {Using=xpath, value=//button[text() = 'Esqueceu seu e-mail?']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at org.openqa.selenium.support.ui.ExpectedConditions.apply(ExpectedConditions.java:205)
    at org.openqa.selenium.support.ui.ExpectedConditions.apply(ExpectedConditions.java:201)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
    at pageobjects.BaseClass.waitVisibility(BaseClass.java:21)
    at pageobjects.BaseClass.isDisplayed(BaseClass.java:51)
    at pageobjects.MaintenanceTimeLinePages.visualizarElementoTelaGoogle(MaintenanceTimeLinePages.java:109)
    at step_definitions.LoginSteps.exibeTelaPadraoLoginContaGoogle(LoginSteps.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at cucumber.runtime.Utils.call(Utils.java:32)
    at cucumber.runtime.Timeout.timeout(Timeout.java:16)
    at cucumber.runtime.Utils.invoke(Utils.java:26)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:37)
    at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:40)
    at cucumber.api.TestStep.executeStep(TestStep.java:102)
    at cucumber.api.TestStep.run(TestStep.java:83)
    at cucumber.api.TestCase.run(TestCase.java:58)
    at cucumber.runner.Runner.runPickle(Runner.java:80)
    at cucumber.runtime.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:140)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:68)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:23)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:73)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:117)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:55)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at cucumber.api.junit.Cucumber.evaluate(Cucumber.java:126)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

这个错误信息...

Starting ChromeDriver 2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a) on port 39113
Only local connections are allowed.
dez 12, 2019 4:25:32 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMAÇÕES: Detected dialect: OSS
    Dado que o usuário acesse a url do sistema SMAPP Parts and Maintenance          # LoginSteps.queOUsuarioAcesseUrlSistemaSMAPP()
    Entao exibe tela padrão de login por conta Google                               # LoginSteps.exibeTelaPadraoLoginContaGoogle()
      org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //button[text() = 'Esqueceu seu e-mail?'] (tried for 5 second(s) with 500 milliseconds interval)
.
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[text() = 'Esqueceu seu e-mail?']"}
  (Session info: chrome=78.0.3904.108)
  (Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 10.0.17763 x86_64) (WARNING: The server did not provide any stacktrace information)
.
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-AJU29UT', ip: '192.168.12.226', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_221'
Driver info: org.openqa.selenium.chrome.ChromeDriver

...暗示 ChromeDriver 无法与 Browsing Context 通信,即 Chrome 浏览器 会话。

您的主要问题是您使用的二进制文件版本之间不兼容,如下所示:

  • 您正在使用 chromedriver=2.43
  • chromedriver=2.43 的发行说明清楚地提到了以下内容:

Supports Chrome v69-71

  • 您正在使用 chrome=78.0
  • ChromeDriver v78.0 的发行说明清楚地提到了以下内容:

Supports Chrome version 78

  • 您的 Selenium 客户端 版本是 3.141.59.

因此 Selenium Client v3.141.59ChromeDriver v2.43Chrome浏览器v78.0


解决方案

确保:

  • Chrome驱动程序 已更新至当前 ChromeDriver v79.0 级别。
  • Chrome 已更新为当前 Chrome 版本 79.0 级别。 (根据 ChromeDriver v79.0 release notes
  • 通过 IDE 和 [=32] 清理 您的 项目工作区 =]重建你的项目只需要依赖。
  • 如果您的基础 Web Client 版本太旧,请卸载并安装最新的 GA 和发布版本的 Web Client
  • 系统重启
  • 非 root 用户身份执行您的 @Test
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例.