user3107875
user3107875

Reputation: 1

Selenium webdriver : exclude child node

Here is the sample HTML Code :

  <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>

I am using selenium webdriver. I have received the all the child elements from this code but now I want to exclude one particular child element in logic, how I can exclude one of the child element from my array. I want to exclude tr[6] child element..

   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);
 } 

Thanks in Advance!!

Upvotes: 0

Views: 1318

Answers (2)

Jens Erat
Jens Erat

Reputation: 38732

Either omit the 6th table row, then select all descendants:

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

or only select all table rows that are not at position 1 and have an attribute (and then select their descendants):

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

or to be more specific, also check the attribute name:

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

Upvotes: 1

Ben
Ben

Reputation: 667

Is it always element 6 that you want to avoid? If it is, use a for look with an increment and just avoid element 6 with an if statement.

int numOfElements = driver.findElements(By.xpath("/html/body/table/tbody  //*")).count();

ArrayList<String> al1 = new ArrayList<String>();
for(int i = 1; i<= numOfElements; i++) 
{
   if(i!=6)
   {
       String className = driver.findElement(By.xpath("/html/body/table/tbody/tr["+i+"]")).getAttribute("class");
       System.out.println("Class name = "+className);
       al1.add(className);
   }
}

This wont sound like a solution that you are looking for, but it still is a round about way to achieve what you want. Off the top of my head, I cant think of another way unless you have a attribute that contains something to compare off of or to exclude

Upvotes: 0

Related Questions