问题
THe element is defined like this:
<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 class is the parent class and "lable" and "select" class is the child class.
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.
I am capturing all the lable class elements in one WebElement LIst,
below is my code:
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
}
}
}
Help pelase..
回答1:
Use the following xpath
which will find the following-sibling
select
if (label.getText().equals("Target Environment"))
{
label.findElement(By.xpath("./following-sibling::select[1]")).click();
}
Or
if (label.getText().equals("Target Environment"))
{
label.findElement(By.xpath("./following::select[1]")).click();
}
来源:https://stackoverflow.com/questions/61097750/for-a-parent-element-there-are-two-child-elements-if-text-of-child-1-matche