Xpath代码在Katalon中工作,但在Selenium Java中不起作用



我使用的是Chrome插件,它工作得很好。但是我想在Java中用Selenium创建一个小应用程序来做类似的事情。我遇到了xpath的问题,它与chrome上的Katalon一起工作,但当我在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中添加了特殊字符,我猜这是在您复制粘贴时发生的。请使用下面的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]"));

相关内容

最新更新