For a parent element, there are two child elements. If .text() of child 1 matches, how to perfome action on Child2?

+1 vote

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
}
}
}

Help pelase..

Apr 8 in Selenium by Jyra
• 490 points
112 views
You could probably use contains function in xpath for this purpose.
not working, any other alternative ..like using "following-sibling"

Yes you could use following-sibling for the same. 

label.findElement(By.xpath("./following-sibling::select")).click();

Thanks, Kim.

On the same note for a table kind of Element structure..as given below, how to apply the following-sibling? If the table header matches with the input string, enter the value in its corresponding text box.

<div class ="table-class">

<table class ="table-response">

<thead>

<tr>

<th class = "ng-binding" >testcase</th>

<th class="ng-binding"> environment</th>

<th class="ng-binding"> source folder</th>

</tr>

</thead>

<tbody>

<tr class ="ng-scope">

<td class="ng-input">

<input class="ng-scope-input" id='testcaseid">

</td>

<td class ="ng-input">

<input class="ng-scope-input" id="environmentid">

</td>

<td class ="ng-input">

<input class="ng-scope-input" id="source folder">

</td>

</tr>

</tbody>

</table>

TIn UI, this will be exactly in below format:

//Table Header

TestCAse ID   Environement  Source folder

<text box>      <text box>       <Text box>

TestCAse ID Environement  Source folder
<text box> <text box> <text box>

Hey @Jyra, could you please post your query as a new question?
HI Sirajul, Sorry, I ahve posted the question 2 days back, it is still is review and yet to be approval status. What shoudl i do, so that everyone can view the question. I ahve to submit my assignemnt today. ALmost 90% complete, struck with this problem. Need assistance plz

1 answer to this question.

+2 votes
Best answer

For your requirement, you could try using the following:

label.findElement(By.xpath("./following-sibling::select")).click();

This should do!

answered Apr 9 by Kim

selected Apr 30 by Sirajul

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

How to print text from a list of all web elements with same class name in Selenium?

@Rajni, following test case is somewhat similar ...READ MORE

answered Jun 20, 2019 in Selenium by Avantika
11,406 views
+2 votes
1 answer

How to fetch the text of a web element where the text is split in 2 lines?

Hello, You want to use a non-breaking space ...READ MORE

answered 2 days ago in Selenium by Niroj
• 22,110 points
49 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
2,594 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,870 points
2,047 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,870 points
2,145 views
+1 vote
1 answer

How to get the following-sibling element for a table type of structure

Why do you want to use the ...READ MORE

answered Apr 9 in Selenium by Karan
• 10,080 points
81 views