How to find the xpath for which have the same name and the the position changes dynamically?

Answered

Hi Everyone,
Can anyone provide me solution for the below one.
I want to get the text under <td> which is dynamic(this will change everytime).Below is the xpath

1)<div class="abc">

<div class="ypath">------</div>

<form method="get" name="frm">

<table>

<tbody>

<tr>

<td>zyx</td>

</tr>

</tbody>

</table>

</form>

</div>
2)

<div class="abc">

<div class="xpath">------</div>

<div class="error"></div>

<form method="get" name="frm">

<table>

<tbody>

<tr>

<td>xyz</td>

</tr>

</tbody>

</table>

</form>

</div>

1 and 2 position may change. Either 1st will come on upper side or second will come on upper side.This will change dynamically.

Add Comment
  • 2 Answer(s)
    Best answer

    Hi.
    I got the solution for the above one.Thank you @amitjain  but that’s not working for me as the table position may change on condition.
    The below one is working fine .

    //div[@class=’xpath’]/following-sibling::form/table/tbody/tr[1]//td
    and

    //div[@class=’ypath’]/following-sibling::form/table/tbody/tr[1]//td

    Train Answered on May 21, 2015.
    Add Comment
    
    Try this generic code
    @Test
    
    public void table(){
    
    driver.get("file:///C:/Users/Desktop/sel/usb_driver/table.htm");
    
    driver.manage().timeouts().implicitlyWait(20L,TimeUnit.SECONDS);
    
    WebElement table = driver.findElement(By.xpath("//table[2]/tbody"));
    
    List<WebElement> allRows1 = table.findElements(By.tagName("tr"));
    
    for (WebElement col : allRows1){
    
    //System.out.println(col.getText() + "\t");
    
    }
    
    for (WebElement row : allRows1) {
    
    List<WebElement> allcols = row.findElements(By.tagName("td"));
    
    for (WebElement col : allcols){
    
    System.out.println(col.getText() + "\t");
    
    }
    
    //System.out.println("\n");
    
    }
    
    }
    
    
    Supporter Answered on May 13, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.