Selenium 2 - Can findElement(By.xpath) be scoped to a particular element?

后端 未结 5 1119
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 12:21

All the examples of findElement(By.xpath) I\'ve seen search the whole page, e.g.

WebElement td = driver.findElement(By.xpath(\"//td[3]\"));

5条回答
  •  暖寄归人
    2021-01-11 12:55

    what i m understanding that u want to retrieve a particular td from a tr, so here's a snippet you can try it with your code to find 3rd td...
    
    
    
    WebElement tr=//...find a particular table row 
        List columns = tr.findElements(By.tagName("td"));
        Iterator j = columns.iterator();
        int count=0;
        while(j.hasNext())  
                {   
                    WebElement column = j.next();
                    if(count==2)
                    {
                        System.out.print(column.getText());
                    }
                    count++;
                }
    
    You can change count value in if condition to retrieve another td..
    Hope this will help you..
    

提交回复
热议问题