java - Selenium 网络驱动程序 : exclude child node

标签 java selenium xpath selenium-webdriver

这里是示例 HTML 代码:

  <table width="100%" cellspacing="0" cellpadding="0" border="0">
   <tbody>
     <tr>
     <tr class="tinyfont">
     <tr height="2px">
     <tr height="1px">
     <tr height="1px">
     <tr>
     <tr height="2px">
     <tr height="1px">
     <tr height="1px">
     <tr height="2px">
  </tbody>
 </table>

我正在使用 selenium webdriver。 我已收到此代码中的所有子元素,但现在我想在逻辑中排除一个特定的子元素,如何从数组中排除该子元素之一。 我想排除 tr[6] 子元素..

   List<WebElement> list = driver.findElements(By.xpath("/html/body/table/tbody  //*"));        

 ArrayList<String> al1 = new ArrayList<String>();

for(WebElement ele:list){
 String className = ele.getAttribute("class");
 System.out.println("Class name = "+className);
    al1.add(className);
 } 

提前致谢!!

最佳答案

忽略第 6 个表格行,然后选择所有后代:

/html/body/table/tbody/tr[position() != 6]//*

或者只选择所有不在位置 1 且具有属性的表行(然后选择它们的后代):

/html/body/table/tbody/tr[position() = 1 or @*]//*

或者更具体地说,还要检查属性名称:

/html/body/table/tbody/tr[position() = 1 or @height or @class]//*

关于java - Selenium 网络驱动程序 : exclude child node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21945377/

相关文章:

c# - .NET Core - Selenium RemoteWebDriver - PlatformNotSupportedException

python - 弃用警告 : use options instead of chrome_options error using ChromeDriver and Chrome through Selenium on Windows 10 system

python - 如何点击以下网站的按钮?

xml - odoo/openERP中子节点的Qweb Xpath?

java - Java ObjectInputStream提供了java.io.EOFException

java - 合并具有相同日期和 ID 的两个列表中的所有元素并总结它们的成本

java - Xalan XPathAPI遍历问题

xpath - 检查 xpath 中两个序列是否包含相同的数字

java - 使用 DateFormat 将字符串转换为日期

java - hibernate ID增量oracle