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 8 in Selenium by Jyra
• 490 points
84 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 by Karan
• 10,080 points

selected Apr 11 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,870 points
9,917 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,180 points
3,908 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
103 views
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,611 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,055 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,164 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 in Selenium by Karan
• 10,080 points
2,015 views
+2 votes
1 answer