Selenium-Webdriver JAVA:- 我收到错误" "org.openqa.selenium.NoSuchElementException: 没有这样的元素" ""但存在约会图标



我想验证工具提示,但没有这样的元素出现错误。我已经确认该元素存在。

爪哇代码:

String toolTipTextAppointment = driver
                .findElement(By
                        .id("//*[@id='EditView_NOTE_POPUP']/table/tbody/tr[2]/td/table/tbody/tr[3]/td[2]/table/tbody/tr/td[1]/a/img")).getAttribute("title");
System.out.println(toolTipTextAppointment);

网页代码:

<td nowrap="nowrap" style="border:0px;">
<a class="" href="javascript:void(0);" onclick="showPopupActivity('Meetings','activityPopupFormAraContent',440,600);">
<img style="border: 6px none;" title="Appointment" src="themes/AutoAccelerator/images/calender_icon.gif"/>
</a>
</td>

尝试

driver.findElement(By.cssSelector("img[src*='calender_icon.gif']")).getAttribute("title")

您使用了findElement(By.id(")),但是您在其中传递了xpath,这就是它不起作用的原因

String toolTipTextAppointment = driver.findElement(By.xpath("/html/body/table/tbody/tr/td/a/img")).getAttribute("title");System.out.println(toolTipTextAppointment);

问题是可见性。有两个不同的概念,存在和可见性(可单击或查看)。

您需要检查该元素是否可见,不确定语法,因为我使用 clojure 库 (clj-webdriver),但据我所知应该是这样的

e=driver.findElement(By.id("idOfElement")).isDisplayed();

考虑到驱动程序会找到隐藏的元素,但它们是不可见的。在这种特殊情况下,您可能需要向下滚动页面以使元素可见,我建议检索其e.location并将坐标与javascript片段一起使用

((JavascriptExecutor)driver).executeScript("window.scrollTo(" + e.location + ")");

然后该元素将是可见的,您将能够与之交互,我通常将此代码嵌入到帮助程序函数中,因为这是一个非常普遍的问题。

免责声明:代码只是一个方向,我不知道语法,因为我不使用Java。希望对你有帮助

最新更新