2014-02-21 35 views
0

这里是HTML代码示例:硒的webdriver:排除子节点

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

我使用硒的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); 
} 

在此先感谢!

+0

我想这可能是你要找的东西? http://stackoverflow.com/questions/7115917/xpath-select-node-but-not-specific-child-elements – Richard

+0

没有与硒webdriver工作.. – user3107875

+0

你想排除哪个元素?您正在使用的html样本将有所帮助。 –

回答

0

它总是元素6,你想避免?如果是这样,使用a用增量来查看,并用if语句避免元素6。

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

这不会听起来像你正在寻找一个解决方案,但它仍然是一个约的方式来实现你想要的一轮。关闭我的头顶,我想不出其他办法,除非你有一个包含一些比较过的或排除

+0

Thanks..can使用解决方案的一部分../html/body/table/tbody/tr [position()!= 6] // *为我做了诀窍.. – user3107875

1

要么省略第六表行中的属性,然后选择所有后代:

/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]//* 
+0

谢谢..这对我有很大的帮助。 – user3107875

+0

如果它解决了你的问题,你可以考虑接受答案,看看[FAQ]。 –