0

I'm having an issue with Selenium in Java. I have a web page like this:

<html>
<body>
<div id='content'>
<table class='matches'>
<tr id='today_01'>
<td class='team-a'>Real Madrid</td>
<td class='score'>0-0</td>
<td class='team-b'>Barcelona</td>
</tr>
<tr id='today_02'>
<td class='team-a'>PSG</td>
<td class='score'>1-1</td>
<td class='team-b'>Manchester City</td>
</tr>
<tr id='today_03'>
<td class='team-a'>Liverpool</td>
<td class='score'>2-2</td>
<td class='team-b'>Arsenal</td>
</tr>
</table>

<div id='content'>
<body>
<html>

I first get all the rows into a list:

List<WebElement> allRows = driver.findElements(By.xpath("//table[@class='matches']/tbody/tr[contains(@id, 'today')]"));

Next I iterate through all the elements displaying the WebElement (i.e. the row) and on the next line I display the td containing the home team, separated by a line:

for (WebElement row : allRows) {
    System.out.println("Outer HTML for row" + row.getAttribute("outerHTML"));
    System.out.println("Outer HTML for Home Team cell" + row.findElement(By.xpath("//td[contains(@class,'team-a')]")).getAttribute("outerHTML"));
System.out.println("------------------------------------------------------------");
}

The first println displays all rows, one by one. The second however displays ONLY 'Real Madrid' for each iteration. I'm losing my mind because I don't understand why. Can someone please help?

The output:

<tr id='today_01'>
<td class='team-a'>Real Madrid</td>
<td class='score'>0-0</td>
<td class='team-b'>Barcelona</td>
</tr>
<td class='team-a'>Real Madrid</td>
------------------------------------------------------------
<tr id='today_02'>
<td class='team-a'>PSG</td>
<td class='score'>1-1</td>
<td class='team-b'>Manchester City</td>
</tr>
<td class='team-a'>Real Madrid</td>
------------------------------------------------------------
<tr id='today_03'>
<td class='team-a'>Liverpool</td>
<td class='score'>2-2</td>
<td class='team-b'>Arsenal</td>
</tr>
<td class='team-a'>Real Madrid</td>
------------------------------------------------------------

1 Answer 1

1

You have to use like this

  System.out.println("Outer HTML for Home Team cell" + row.findElement(By.xpath("td[contains(@class,'team-a')]")).getAttribute("outerHTML"));

Then it will point to the correct element that we want.

Sign up to request clarification or add additional context in comments.

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.