0

I am trying to print the values of the City column for different rows from the below table shown in the image.

I used the below code. My code prints "Dubai" 4 times instead of printing different cities. Can someone help me in fixing this?

System.setProperty("webdriver.chrome.driver","C:\\Selenium\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get("http://toolsqa.com/automation-practice-table/");


WebElement body = driver.findElement(By.xpath("//*[@id='content']/table/tbody"));
List<WebElement> rows = body.findElements(By.tagName("tr"));
System.out.println(rows.get(0).findElement(By.xpath("//td[2]")).getText());
System.out.println(rows.get(1).findElement(By.xpath("//td[2]")).getText());
System.out.println(rows.get(2).findElement(By.xpath("//td[2]")).getText());
System.out.println(rows.get(3).findElement(By.xpath("//td[2]")).getText());

Table Image

0

2 Answers 2

1

Actually your code is correct, only problem with your xpath using for getting city element.

When you are going to find city name using xpath //td[2], your are searching actually second column every time but on whole page that's why you are getting same city name every time.

You need to provide .//td[2] xpath, because when you provided xpath with . it will search only element context otherwise it will search on document means on whole page, and according to whole page output is absolutely correct.

Now if you want simply print all cities try as below :-

List<WebElement> cities = driver.findElements(By.xpath("//*[@id='content']/table/tbody//td[2]")); 

for(WebElement city : cities) 
{
 System.out.println(city.getText());
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Saurabh for the detailed explanation. Now, my code works perfectly.
@Saurabh I'm just curious, if we provide // in the xpath like you have given /tbody//td[2] skips one element (say tr in this case)? Also, will my answer work too for printing City column values? I'm not an expert in Selenium. So, I was just curious to know.
@Harish / Selects from the root node and // Selects nodes in the document from the current node that match the selection no matter where they are, so when we are at tbody I'm just consider it current node then after that I used // means now locator will look for element from the tbody...
0

Try below code to print values of the City column:

    List<WebElement> rows = driver.findElements(By.xpath("//*[@id='content']/tbody/tr"));
    for(int row = 0; row < rows.size(); row++){
        System.out.println(driver.findElement(By.xpath("//*[@id='content']/table/tbo‌​dy/tr[row]/td[2]")).getText());
    }

Hope it helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.