2014-12-19 45 views
1

特定元素我有这样的HTML代码:硒的webdriver(爪哇) - 选择取决于条件

<div class="ex1"> 
    <div class="ex2"> 
     <span>test1</span> 
     <span class="ex3">test2</span> 
    </div> 
    <div class="ex2"> 
     <span>test3</span> 
     <span class="ex3">test2</span> 
    </div> 
</div> 

我使用硒的webdriver。 我需要创建Java代码可能:

如果<span>test3然后选择一个<span class="ex3">其位于内同一div class="ex2" 但因为我有div的,跨越一个主内相同的className我无法区别这跨越。 。

请问这个问题请你帮忙吗?

所以,这样的事情:

如果<span>test3 then <span class=ex3>test2. 或 如果<span>test1 then <span class=ex3>test2.

感谢

+0

请将其中的任意一个回复标记为对您有帮助的回答。谢谢.. :) – Subh 2014-12-19 13:47:43

回答

1

1-使用这个XPath去<span>test1 then <span class=ex3>test2

//span[.='test1']/following-sibling::span[.='test2'] 

而且,我们E在这样的代码:

WebElement ele = driver.findElement(By.xpath("//span[.='test1']/following-sibling::span[.='test2']")); 


2,而且,使用这个XPath去<span>test3 then <span class=ex3>test2

//span[.='test3']/following-sibling::span[.='test2'] 

使用在这样的代码:

WebElement ele = driver.findElement(By.xpath("//span[.='test3']/following-sibling::span[.='test2']")); 
0

我已经按照您的要求实施了If语句在实践中你可以根据你的要求修改它。

WebElement test1_class1 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='ex1']//div[1]//span[1]"))); 
String test1= test1_class1.getText(); 
WebElement test2_class1 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='ex1']//div[1]//span[2]"))); 
String test2_1 =test2_class1.getText(); 
WebElement test3_class2 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='ex1']//div[2]//span[1]"))); 
String test3 =test3_class2.getText(); 
WebElement test2_class2 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='ex1']//div[2]//span[2]"))); 
String test2_2 =test3_class2.getText(); 
try 
{ 
if(test1.equals("test1")) 
        { 
        System.out.println(test2_1); 
        } 
if(test3.equals("test3")) 
        else 
        { 
        System.out.println(test2_2); 
        } 

} 
catch(Throwable e) 
{ 
System.out.println("Exception in program"+e); 
}