【问题标题】:How to fix exception NoSuchELement in eclipse selenium project?如何修复 eclipse selenium 项目中的异常 NoSuchELement?
【发布时间】:2017-04-10 14:18:33
【问题描述】:

当尝试在 Eclipse 项目中使用 Maven 项目中的 ie 驱动程序通过 x 路径查找元素时发生异常(元素未找到@给定 xpath),该元素在 iframe 内, 检查所需元素时,我发现它位于以下结构中:

 <div id="idName">
   <div class="container">
     <div>
       <p class="v1">..............notice this repeated for many different href
         <a href= 
          <span class="v2".........notice this repeated for many different href
           <b>part of label</b>
               rest of label
               <br></br>

当我使用时

webelement.findElement(By.xpath("//*[@id='idName']/div/div/p/a")).getAttribute("href"))

这会在运行No Such Element Exception 时导致异常 ,似乎我错过了相对XPath中的某些内容,有什么想法吗?

注意:我尝试在查找元素之前将驱动程序切换到 iframe,但它没有帮助并且仍然有异常。

非常感谢您的帮助。

【问题讨论】:

  • 检查你的&lt;div id="idName"&gt;是否位于iframe/frame中
  • @MaiSaad href 是a 的属性,而不是p,因此,至少您应该尝试以下XPath://*[@id='idName']/div/div/p/a
  • @har07:我试过了,但给出了同样的异常!
  • @Andersson:是的,我检查过,它位于 iframe 内,这有什么帮助?
  • @Andersson :即使在使用添加切换到框架之后

标签: javascript xpath selenium-webdriver junit4


【解决方案1】:

当我们想要获取它的属性的元素存在于 iframe 中时,我们将首先切换到该框架: driver.switchTo().frame("frameid").findElement(By.xpath("element xpath")).getAttribute("href"); 然后切换到默认内容以超出框架: driver.switchTo().defaultContent();

【讨论】:

    【解决方案2】:

    根据上面提供的 HTML,最好的办法是在 div 'ID' 之后使用第一个锚标记,假设 ID 在您正在处理的整个页面中是唯一的。

    如下修改定位器并试一试:

    //*[@id='idName']//following::a

    如果这会在您的页面中返回超过 1 个链接,那么您可以在末尾添加一个索引,如下所示:

    (//*[@id='idName']//following::a)[1]

    【讨论】:

    • 不幸的是我仍然有异常 org.openqa.selenium.NoSuchElementException: 没有这样的元素: 无法找到元素: {"method":"xpath","selector":"//*[@id ="idname"]/]/div/div/p::a"}
    猜你喜欢
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2019-10-31
    相关资源
    最近更新 更多