对于父元素,有两个子元素.如果子项1的.text()匹配,如何对子项2执行操作 [英] for a parent Element, there are two child Elements. If .text() of child 1 matches, how to perfomr action on Child2
本文介绍了对于父元素,有两个子元素.如果子项1的.text()匹配,如何对子项2执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
元素定义如下:
<div class ="frm-group'>
<label class ="ng.binding" "Source Environment" ..... </label>
<select class "ng-touched" ...... </select>
</div>
<div class ="frm-group'>
<label class ="ng.binding" "Target Environment" ..... </label>
<select class "ng-touched" ...... </select>
</div>
Div类是父类,"lable"和"select"类是子类.
Div class is the parent class and "lable" and "select" class is the child class.
问题陈述:我必须执行以下操作::如果Label.getText()与字符串匹配,请单击其各自的选择类并执行操作.
Problem Statement : I have to do the following :: If the Label.getText() matches with a string, then click on its respective select class and perform action.
我正在一个WebElement LIst中捕获所有可能的类元素,
I am capturing all the lable class elements in one WebElement LIst,
下面是我的代码:
public void add inputs(){
// here I will get two elements int the list
List<Webelement> labels = BrowserFactory.getdriver().findElements(By.xPath("//div/label[@class='ng.binding']")
// here I will get two elements related to select in the list
List<Webelement> selectDropdown= BrowserFactory.getdriver().findElements(By.xPath("//div/select[@class='ng-touched']")
for (Webelement label: labels )
{
if (label.getText().equals("Target Environment"))
{
// here I have to click the select(dropdown) of the matched label i.e Target Environement' select class
}
}
}
求助.
推荐答案
使用以下xpath
可以找到following-sibling
选择
if (label.getText().equals("Target Environment"))
{
label.findElement(By.xpath("./following-sibling::select[1]")).click();
}
或
if (label.getText().equals("Target Environment"))
{
label.findElement(By.xpath("./following::select[1]")).click();
}
这篇关于对于父元素,有两个子元素.如果子项1的.text()匹配,如何对子项2执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文