【问题标题】:Xpath code is working in Katalon but doesn't work in Selenium JavaXpath 代码在 Katalon 中有效,但在 Selenium Java 中无效
【发布时间】:2021-12-09 05:23:09
【问题描述】:

我正在使用适用于 Chrome 的 Katalon 插件,效果很好。但我想用 Java 中的 Selenium 创建一个小应用程序来做类似的事情。我遇到了在 Chrome 上与 Katalon 一起使用的 xpath 的问题,但是当我在 Selenium 的 Java 应用程序中使用它时它不起作用。

这是来自 Katalon 的 xpath:

xpath=(.//*[normalize-space(text()) and normalize-space(.)='Incorrect: Type your incorrect message here'])[1]/following::label[1]

这就是我在 Java 中与 Selenium 一起使用的:

WebElement el = driver_1.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)=\'Incorrect: Type your incorrect message here\'])[1]/following::label[1]"));

返回:

Exception in thread "AWT-EventQueue-0" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:

【问题讨论】:

  • 你能从浏览器复制xpath并粘贴到这里吗?

标签: java selenium web browser


【解决方案1】:

特殊字符 \ 正在添加到您的 xPath 中,我想这是在您复制粘贴时发生的。请使用下面的xPath

driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Incorrect: Type your incorrect message here'])[1]/following::label[1]"));

如果您仍然无法访问元素,请使用一些 Explicit 等待语句。

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(
            "(.//*[normalize-space(text()) and normalize-space(.)='Incorrect: Type your incorrect message here'])[1]/following::label[1]")));
    driver.findElement(By.xpath(
            "(.//*[normalize-space(text()) and normalize-space(.)='Incorrect: Type your incorrect message here'])[1]/following::label[1]"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-01
    • 2016-03-09
    • 2018-11-14
    • 2021-02-11
    • 1970-01-01
    • 2022-01-23
    • 2021-06-06
    • 2012-09-21
    相关资源
    最近更新 更多