Selenium’s Relative Locator

177 Views Asked by At

I am trying to get values from our application using selenium’s Relative Locator. A small sample of data look like this. In actual env, there are hundreds of records with each row having three columns with Fields name, separator, and value. Each row got dynamic id. Field Name got dynamic id. Class name of value fields are same. I was trying to loop through the data by passing field names. For each loop, I am only getting the first value, ie “122123000”. I should have got 122123000, then 5866 and so on..

Name Separator Value
Number : 122123000
Sender : 5866
Receiver : 9741401
Branch : 1050
Date : 11.11.2022
Time : 12:30:51:00
Version : B.1.8
Register dt : 10.10.2022
Register no : ATD403005131111225866

My code is.

for(int cf=0;cf<fields.length;cf++) {
            By values = RelativeLocator.with(By.tagName("td")).toRightOf(By.xpath("//*[contains(text(), '"+fields[cf]+"')]")).near(By.xpath("//label[contains(@class, 'ui-outputlabel ui-widget msgValue')]"));
            String textValues=driver.findElement(values).getText();
            System.out.println(textValues);
        }

Appreciate any help.

1

There are 1 best solutions below

1
On

If you simply wish to retrieve the elements from the 3rd column of the table then the following will suffice:

List<WebElement> cells = driver.findElements(By.xpath(String.format("//table/tbody/tr/td[position()=%d]", 3)));
for(WebElement cell : cells) {
    System.out.println (cell.getText());
}