我尝试从自动建议字段中选择一个元素,但总是出现错误,提示即使我确定我的 xpath 是正确的,也无法找到该元素
这是我的代码:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class=\"ui-menu-item-with-icon ui-menu-item\"][1]")));
driver.findElement(By.xpath("//*[@class=\"ui-menu-item-with-icon ui-menu-item\"][1]")).click();
它应该找到//* @class=\"ui-menu-item-with-icon ui-menu-item\"这是阿尔伯特·卡姆斯的第一个建议
这是外部Html
<li class="ui-menu-item-with-icon ui-menu-item" role="menuitem">
<a class="ui-corner-all" tabindex="-1">
<span class="item-icon"></span>
Albert Camus (SARCELLES)</a>
</li>"
最佳答案
除了使用通配符可能会导致处理时间更长之外,您的 XPath 或多或少都可以,因此您可以使用 li
而不是*
。
另一个选择是坚持 <a>
tag containing the text您想使用 normalize-space() function 单击像这样:
//a[normalize-space()="Albert Camus (SARCELLES)"]
此外,您的弹出窗口可能位于 iframe 内所以你可能必须 switch the webdriver context到相关的 iframe 元素。
关于java - 即使 xpath 正确也无法找到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56152491/