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

huangapple go评论87阅读模式
英文:

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 &quot;lable&quot; and &quot;select&quot; 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(&quot;Target Environment&quot;))
    		{
    		   label.findElement(By.xpath(&quot;./following-sibling::select[1]&quot;)).click();
    		}


Or

    if (label.getText().equals(&quot;Target Environment&quot;))
    		{
    		   label.findElement(By.xpath(&quot;./following::select[1]&quot;)).click();
    		}

</details>



huangapple
  • 本文由 发表于 2020年4月8日 17:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61097750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定