[英]Selenium Webdriver (Java) - selecting specific element depending on the condition
我有類似的HTML代碼:
<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>
我正在使用Selenium 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.
謝謝
1-使用此xpath到達<span>test1 then <span class=ex3>test2
:
//span[.='test1']/following-sibling::span[.='test2']
並且,在這樣的代碼中使用:
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']"));
實際上,我已經根據您的問題實施了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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.