【问题标题】:Selenium returning error only in headless mode C#Selenium 仅在无头模式 C# 中返回错误
【发布时间】:2021-01-25 19:51:08
【问题描述】:

到目前为止,我所有的测试都运行良好,为了提高速度,我试图在无头模式下运行所有​​测试,但是其中一些测试失败了,其中一个出现以下错误:

  OpenQA.Selenium.ElementNotInteractableException: element not interactable
      (Session info: headless chrome=87.0.4280.141)
  Rastreamento de Pilha: 
    RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
    RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary'2 parameters)
    RemoteWebElement.Execute(String commandToExecute, Dictionary'2 parameters)
    RemoteWebElement.Click()

返回该错误的行是

Driver.FindElement(By.CssSelector("#page_content_inner > div.uk-grid > div > div:nth-child(2) > div > div > div > ul > li:nth-child(2) > a")).Click();

其他点击被拦截:

      (Session info: headless chrome=87.0.4280.141)
  Rastreamento de Pilha: 
    RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
    RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary'2 parameters)
    RemoteWebElement.Execute(String commandToExecute, Dictionary'2 parameters)
    RemoteWebElement.Click()

产生错误的行是:

Driver.FindElement(By.Id("ClientesConvenio")).Click();

我真正的问题是:无头模式有什么限制吗?有没有我应该使用无头模式的初学者知识?为什么会这样?

【问题讨论】:

  • 不是解决方案,但无头模式与完整浏览器的行为不同的问题很常见。一般来说,我们使用的解决方案是简化选择器,理想情况下是通过更新底层应用程序来公开易于定位的“数据”属性。复杂的 CSS 选择器非常脆弱,需要随着应用程序的变化而不断更新。使用特定于测试的目标将导致更健壮的测试。

标签: c# selenium selenium-webdriver selenium-chromedriver google-chrome-headless


【解决方案1】:

正如@Nick Bailey 在他的 cmets 中提到的那样 ...headless 模式的行为与完整浏览器不同... 由于:


解决方案

理想情况下,Click() 必须在元素上诱导 WebDriverWaitElementToBeClickable(),您可以使用以下任一 Locator Strategies

  • 身份证

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.Id("ClientesConvenio"))).Click();
    
  • CssSelector

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("#page_content_inner > div.uk-grid > div > div:nth-child(2) > div > div > div > ul > li:nth-child(2) > a"))).Click();
    

参考文献

您可以在以下位置找到一些相关的详细讨论:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 2021-10-24
    • 1970-01-01
    • 2021-11-22
    • 2021-03-31
    • 1970-01-01
    • 2022-07-06
    相关资源
    最近更新 更多