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, 2020 in Selenium by Jyra
• 580 points
5,925 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, 2020 by Kim

selected Apr 30, 2020 by Sirajul

Related Questions In Selenium

0 votes
2 answers

Is there any way to get the text of a web element using Selenium Webdriver?

use gettext() in java : string lableText = ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
8,489 views
0 votes
3 answers

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

Try using List <WebElement> to access all similar elements ...READ MORE

answered Dec 16, 2020 in Selenium by Roshni
• 10,480 points
87,452 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 May 28, 2020 in Selenium by Niroj
• 82,840 points
4,420 views
0 votes
1 answer
0 votes
2 answers

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,500 points
13,685 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,148 views
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,910 points
7,226 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,879 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, 2020 in Selenium by Karan
• 19,610 points
4,246 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP