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

时间:2013-12-24 09:13:54

标签: dom selenium selenium-webdriver

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

Java代码:

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);

HTML代码:

<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>

3 个答案:

答案 0 :(得分:1)

尝试

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

答案 1 :(得分:1)

你使用了findElement(By.id(“”)),但你在其中传递了xpath,这就是它无法正常工作的原因

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

答案 2 :(得分:0)

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

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

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

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

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

然后元素将是可见的,您将能够与它进行交互,我通常将此代码嵌入到辅助函数中,因为这是一个非常常见的问题。

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