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

时间:2019-12-12 18:46:46

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

我正尝试在与IAP google配合使用的应用程序上使用无头模式。当无头模式关闭时,它将运行无错误。当无头模式设置为“开”时,硒无法定位元素。

我不知道该如何解决。我为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$7.apply(ExpectedConditions.java:205)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.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$1.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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at cucumber.api.junit.Cucumber$1.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)

1 个答案:

答案 0 :(得分:1)

此错误消息...

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 无法与浏览上下文 Chrome浏览器会话进行通信。

您的主要问题是所使用的二进制版本之间的不兼容性

  • 您正在使用 chromedriver = 2.43
  • chromedriver=2.43的发行说明中明确提到以下内容:
  

支持 Chrome v69-71

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

支持 Chrome 78版

  • 您的 Selenium Client 版本是 3.141.59

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


解决方案

确保:

  • ChromeDriver 已更新为当前的ChromeDriver v79.0级别。
  • Chrome 已更新为当前的 Chrome版本79.0 级别。 (根据ChromeDriver v79.0 release notes
  • 通过您的 IDE
  • 清理您的项目工作区重建您的项目,并且仅具有必需的依赖项。
  • 如果您的基本 Web Client 版本太旧,请卸载并安装最新版本的 Web Client GA和发行版本。
  • 进行系统重启
  • 非root用户用户的身份执行@Test
  • 始终在driver.quit()方法内调用tearDown(){},以优雅地关闭和销毁 WebDriver Web Client 实例。