【问题标题】:Selenium Webdriver: Can't access element containing onclick and text attributesSelenium Webdriver:无法访问包含 onclick 和 text 属性的元素
【发布时间】:2015-08-28 10:32:32
【问题描述】:

我正在使用的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')]"));  

为什么我找不到这个元素?
这里有什么问题?

【问题讨论】:

  • 尝试向元素添加 id 并按 id 查找。元素是否在 iframe 中?
  • 如何为元素添加 id?我正在为此页面编写自动化测试代码,而不是创建页面本身。是的,它在某个 iframe 中。整个 HTML 页面包含几个 iframe

标签: javascript java selenium selenium-webdriver


【解决方案1】:

由于元素在iframe 内,您需要先switch到 iframe:

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

然后找到&lt;a&gt; 元素。

iframe可以通过idxpath或者常用方法找到。

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

driver.switchTo().defaultContent();

【讨论】:

  • 你救了我的命;)谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-02-08
  • 2018-07-02
  • 2021-03-02
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 2023-01-20
  • 1970-01-01
相关资源
最近更新 更多