Tomek Nowakowski
Tomek Nowakowski

Reputation: 21

Iterating on the table using Selenium

I don't understand why x-path doesn't working in table. I'm trying to iter table from the website I found xpath but Eclipse told me:

"Unable to find element with xpath".

Should I find xpath in tables another way?

Website URL: https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie

I try to read one offer for example:

String urlwyniki ="https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie";
driver.get(urlwyniki);
String xpathResult = "//html/body/div[2]/div[4]/div[2]/div[2]/div/div[2]/div[5]/table/tbody/tr[5]/td";
String sCellValue = driver.findElement(By.xpath(xpathResult)).getText();
System.out.print(sCellValue);

What did I do wrong?

Upvotes: 0

Views: 115

Answers (1)

Murthi
Murthi

Reputation: 5347

Find the below code to iterate all cell value in the table. It may help you.

String urlwyniki ="https://www.oferty.net/mieszkania/szukaj?ps%5Blocation%5D%5Btype%5D=1&ps%5Btype%5D=1&ps%5Btransaction%5D=1&ps%5Blocation%5D%5Btext%5D=dolno%C5%9Bl%C4%85skie";
driver.get(urlwyniki);
String xpathResult = "//table/tbody/tr/td";
List<WebElement> rows = driver.findElements(By.xpath(xpathResult));
List<WebElement> cols=new ArrayList<WebElement>();
for(int i=0;i<rows.size();i++){
    System.out.print("Row value: "+i);
    cols=rows.get(i).findElements(By.tagName("td"));
    for(WebElement col:cols)
        System.out.print("cell value "+col.getText());
} 

Upvotes: 1

Related Questions