【问题标题】:Selenium did not accept my xpath expressionSelenium 不接受我的 xpath 表达式
【发布时间】:2020-07-16 08:28:53
【问题描述】:

我试过这个 xpath 来检索一个按钮

By addGhostButton = By.xpath("//button[@data-test-id=\"order\"]");

Selenium 没有找到它,我遇到了这个错误invalid selector: Unable to locate an element with the xpath expression

也许语法错误?请问有什么帮助吗?

【问题讨论】:

  • By.xpath("//button[@data-test-id='order']")
  • 试过了,没用
  • 您确定该元素存在吗?在此处粘贴 dom
  • 给定的 xpath 似乎没有问题,只需添加一个等待,它应该可以正常工作

标签: java selenium xpath


【解决方案1】:

您尚未提供完整的错误堆栈跟踪和相关的 HTML。但是,您构建的 非常有效且合法。因此,您不应该看到 invalid selector 错误,但 Unable to locate an element with the xpath expression 很有可能。

所需元素看起来是一个动态元素,因此要找到您必须为visibilityOfElementLocated() 诱导WebDriverWait 的元素,您可以使用以下任一Locator Strategies

  • 使用cssSelector

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[data-test-id='order']")));
    
  • 使用xpath

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@data-test-id='order']")));
    

【讨论】:

    【解决方案2】:

    试试这个 xpath //button[Contains(text(),"Precommande maintenant")]`

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 2023-03-24
      相关资源
      最近更新 更多