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

+1 vote

 for a table kind of Element structure..as given below, how to apply the following-sibling . If the table hearder 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>
</div>


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

Code is 

public void add inputs(){
// here I will get three elements in the list
   List<Webelement> tlabels = BrowserFactory.getdriver().findElements(By.xPath("//div/table/thead/tr[@class='ng.binding']")
// here I will get three elements related to input box in the list
List<Webelement> ltexts= BrowserFactory.getdriver().findElements(By.xPath("//div/table/tbody/tr/td/input[@class='ng-scope-input']")

for (Webelement label: tlabels )
{
if (label.getText().equals("TestCAse ID"))
{
// here I have to enter the string . //need help how we can use following-sibling for table type of structure.
}
}
}
Apr 9, 2020 in Selenium by Jyra
• 580 points
3,761 views

1 answer to this question.

+2 votes
Best answer

Why do you want to use the following-sibling? for your use case you could probably try the following:

List<WebElement> labels = driver.findElements(By.xpath("//table[@class='table-response']//tr//th"));
        int i=1;
        for (WebElement label: labels )
        {
        if (label.getText().equals("environment"))
         {
           label.findElement(By.xpath("./following::input[" + i + "]")).sendKeys("Environment");
         }
         i=i+1;
        }

Hope this helps!

answered Apr 9, 2020 by Karan
• 19,610 points

selected Apr 11, 2020 by Jyra

Related Questions In Selenium

0 votes
1 answer

How to get next sibling element using XPath and Selenium for Java?

Below code will help you: Try following-sibling axis : WebElement ...READ MORE

answered May 15, 2018 in Selenium by Samarpit
• 5,910 points
20,252 views
+1 vote
2 answers

How to get the title of a webpage using Selenium Java?

Essentially, driver.getTitle(); function can be used to ...READ MORE

answered Dec 17, 2018 in Selenium by Vardhan
• 13,190 points
15,264 views
0 votes
1 answer

How to get the XPath of an element?

To get the xpath of an element ...READ MORE

answered Dec 19, 2018 in Selenium by Nabarupa
538 views
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
12,577 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
8,559 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
6,606 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,506 views
+1 vote
1 answer

How can I automate the process of adding iPhone to cart in Flipkart using Selenium(java),Page Object Model and TestNG? Also validate if product is added and available in cart?

Hey check this https://www.edureka.co/community/47160/automate-purchase-adding-book-cart-flipkart-using-selenium? It deals with a similar ...READ MORE

answered Jan 13, 2020 in Selenium by Karan
• 19,610 points
7,789 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