代码之家  ›  专栏  ›  技术社区  ›  Akhil

在selenium web驱动程序中查找web元素时出现多种情况

  •  -3
  • Akhil  · 技术社区  · 7 年前

    检查元件代码如下所示:

    *<*th class="k-header k-filterable k-with-icon" data-field="Status" data-index="8" data-title="Status" scope="col" style="overflow: visible; white-space: normal;font-weight: bold;width:70px;vertical-align:top;" data-role="columnsorter" id="grdAutoLaborOps_active_cell" aria-describedby="grdAutoLaborOps_active_cell">
        <a class="k-grid-filter" href="javascript:void(0)" tabindex="-1">
        <*span class="k-icon k-filter">
             </span></a><*a class="k-link" href="/tracs/tracs/LaborOps/AutoLaborOps_Read?grdAutoLaborOps-sort=Status-asc" tabindex="-1">Status</a>
    </th>
    

    网格的第10列上有一个过滤器图标,如果我使用下面的代码,我想单击该过滤器图标

            driver.findElement(By.cssSelector("th.k-header.k-filterable.k-with-icon[data-title=Status]")).click();
            driver.findElement(By.cssSelector("span[class*='k-icon k-filter']")).click();
    

    第一行代码单击第10列,将设置为第10列的升序和降序。 第二行代码单击第一列过滤器图标。我的意图是点击第10列过滤器图标

    请帮忙

    1 回复  |  直到 7 年前
        1
  •  1
  •   undetected Selenium    7 年前

    你就快到了。您需要添加 @ 使用 attribute_name data-title 而不是 cssSelector 是的 xpath . 您可以使用以下任一选项:

    • CSS选择器 :

      driver.findElement(By.cssSelector("th.k-header.k-filterable.k-with-icon[data-title=Status]")).click();
      
    • xpath :

      driver.findElement(By.xpath("//th[@class='k-header k-filterable k-with-icon' and @data-title='Status']")).click();
      

    更新:

    如果我已经很好地理解了您的问题更新,请单击以下行 10th column, ascending and descending order ,这是完美的。

    驾驶员findElement(由.cssSelector(“th.k-header.k-filterable.k-with-icon[数据标题=状态]”)创建)。单击();
    

    因此,点击 10th column filter icon 您可以使用以下代码行:

    driver.findElement(By.cssSelector("th.k-header.k-filterable.k-with-icon[data-title=Status] span.k-icon.k-filter")).click();