【问题标题】:Unable to locate element using xpath无法使用 xpath 定位元素
【发布时间】:2016-07-01 18:06:41
【问题描述】:

我对 Selenium 非常陌生,刚刚学到了一些东西并尝试自动化。无法在 Selenium webdriver 中为以下元素找到一个元素:

<div class="navBg withSubMenu">
  <table id="topnav" class="navTable" cellspacing="0" cellpadding="0" style="-moz-user-select: none; cursor: default;">
     <tbody>
        <tr>
           <td class="logoCell navCell" valign="top">
           <td class="navItem navCell relative selected">
           <td class="navItem navCell relative notSelected">
               <a class="content tasks" href="/tasks/otasklist.do">
                  <div class="label" style="z-index:155; ">TASKS</div>
                  <div class="img"> </div>
               </a>
           </td>
           <td class="navItem navCell relative notSelected">
               <a class="content reports" href="/reports/reports.do">
           </td>
           <td class="navItem navCell relative notSelected">
           <td class="menuCell navCell" valign="top">
        </tr>
     <tr class="secondLevelRow">
  </tbody>
</table>

我写过类似的代码

driver1.findElement(By.xpath("//*[Contains(@class,'content tasks')]")).click();

请任何人帮助我。

还请推荐一些站点或链接以了解有关定位器的更多信息,尤其是 xpath。我尝试了一些,但没有深入了解。

提前致谢。

【问题讨论】:

    标签: selenium xpath webdriver


    【解决方案1】:

    contains 以小写 c 开头:

    driver1.findElement(By.xpath("//*[contains(@class, 'content tasks')]")).click();
    

    【讨论】:

    • 另外请推荐一些站点或链接以了解有关定位器的更多信息,尤其是 xpath。我尝试了一些,但没有深入了解。
    • @Harish,如果有帮助,请accept 这个答案。谢谢。
    • 我喜欢XML Path Language (XPath)。这是官方参考,也很平易近人。例如,请参阅2 Location Paths 中列出的示例。
    【解决方案2】:
    <a class="content tasks" href="/tasks/otasklist.do">
    
    driver1.findElement(By.xpath("//a[@class='content tasks']")).click();
    

    【讨论】:

      猜你喜欢
      • 2021-02-11
      • 1970-01-01
      • 2021-08-01
      • 2018-03-15
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多