英文:
for a parent Element, there are two child Elements. If .text() of child 1 matches, how to perfomr action on Child2
问题
以下是翻译好的内容:
元素的定义如下:
```
div类是父类,label和select类是子类。
问题陈述:我需要执行以下操作:如果Label.getText()与某个字符串匹配,则点击其相应的select类并执行操作。
我正在捕获所有label类元素放入一个WebElement列表中,
下面是我的代码:
public void addInputs(){
// 在这里,我会在列表中获取两个元素
List<WebElement> labels = BrowserFactory.getDriver().findElements(By.xpath("//div/label[@class='ng-binding']"));
// 在这里,我会在列表中获取两个与select相关的元素
List<WebElement> selectDropdown = BrowserFactory.getDriver().findElements(By.xpath("//div/select[@class='ng-touched']"));
for (WebElement label : labels )
{
if (label.getText().equals("Target Environment"))
{
// 在这里,我必须点击匹配的标签的select(下拉列表),即目标环境' select 类
}
}
}
请帮帮忙...
<details>
<summary>英文:</summary>
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..
</details>
# 答案1
**得分**: 1
使用以下的 `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();
}
<details>
<summary>英文:</summary>
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();
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论