我需要在span
元素(类“D”)中搜索关键字,然后根据匹配条件,我想点击选择 {{1}元素按钮。
假设我正在搜索“测试”,我希望看到有关如何实现此目的的工作代码。
示例标记:
input
答案 0 :(得分:2)
以下是java
中的代码示例//Get the text from span
String text = driver
.findElement( By.xpath("//span[@class='js-open-more-info']") )
.getText();
//if the text is equal to Test then click on the button
if( text.equals("Test") ){
driver.findElement( By.xpath("//input[@class='C']") ).click();
}
修改强>
用于处理相同结构的多个div样本html(只是调整了你的html并添加了多个div)
<强> HTML 强>
<html>
<body>
<div class="A">
<div class="B">
<input class="C" type="submit" onclick="AddFacility(this)" data-facilityid="300075" value="Choose">
</div>
<div class="D">
<span class="js-open-more-info">Test1</span>
</div>
</div>
<div class="A">
<div class="B">
<input class="C" type="submit" onclick="AddFacility(this)" data-facilityid="300075" value="Choose">
</div>
<div class="D">
<span class="js-open-more-info">Test2</span>
</div>
</div>
<div class="A">
<div class="B">
<input class="C" type="submit" onclick="AddFacility(this)" data-facilityid="300075" value="Choose">
</div>
<div class="D">
<span class="js-open-more-info">Test3</span>
</div>
</div>
</body>
<script>
document.getElementsByClassName('C')[2].addEventListener("click", myFunction);
function myFunction() {
alert('deleted');
}
</script>
</html>
Java中的代码
//get all the divs into a list
List<WebElement> divs = driver.findElements(By.xpath("//div[@class='A']"));
//loop through the list
for(int i=0;i<divs.size();i++)
{
//get text of all the span elements inside multiple divs
String text=divs.get(i).findElement(By.xpath(".//span[@class='js-open-more-info']")).getText();
System.out.println(text);
//if text of span element is equal to Test3 click on the respective delete button
if(text.contentEquals("Test3")){
divs.get(i).findElement(By.xpath(".//input[@class='C']")).click();
}
}
希望这可以帮助你....如果你需要任何进一步的帮助,请退回