for a parent Element, there are two child Elements. If .text() of child 1 matches, how to perfomr action on Child2

时间秒杀一切 提交于 2020-04-11 17:59:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!