Selenium Webdriver:不能访问包含onclick和text属性的元素

Selenium Webdriver: Can't access element containing onclick and text attributes

本文关键字:text 属性 元素 onclick 包含 Webdriver 不能 访问 Selenium      更新时间:2023-09-26

我正在使用的HTML页面包含以下元素:

<a style="text-decoration:none; font-weight:normal;" href="javascript:void(0);" onclick="CreateNewServiceItemApproved();">  
<img src="icons/ui/addnew.png">  
<span style="color:#000">Add New Incident</span>  
</a>  

我需要点击它,但是收到

 org.openqa.selenium.NoSuchElementException: Unable to locate element

错误。
尝试使用以下命令找到它:

driver.findElement(By.xpath("a[@onclick='CreateNewServiceItemApproved()']")).click();  
driver.findElement(By.xpath("//span[contains(text(),'Add New Incident')]"));  

为什么我找不到这个元素?
这是怎么了?

由于元素在iframe中,您需要首先将switch添加到iframe中:

driver.switchTo().frame(driver.findElement(By.id("iframeId")));

然后找到<a>元素。

Iframe可以通过id, xpath或通常的方法找到。

处理完成后,您可能需要使用以下命令切换到默认内容:

driver.switchTo().defaultContent();
相关文章: