Apologies if this is a dumb question - I'm new to Selenium.
I have a web page I'm testing that has a few hypertext links in a table. The HTML looks like this:
<table>
<thead>
<tr>
<td><b>History</b></td>
<td><b>Attributes</b></td>
<td><b>Xml</b></td>
</tr>
</thead>
<tbody>
<tr>
<td><a href=link here>Show</a></td>
<td><a href=link here>Show</a></td>
<td><a href=link here>Show</a></td>
</tr>
</tbody>
</table>
I want to test a click on each of the 'Show' links. They all have the same text, so I can't reference them by linktext. I've been referencing them by XPath, so that:
driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/thead/tr/td[1]").getText()
correctly returns 'History' and
driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/tbody/tr/td[1]").getText()
correct returns 'Show'.
So I would think that:
driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/tbody/tr/td[1]")).click()
would click on the 'Show' link in the first column. But it doesn't - nothing happens.
If I do:
driver.findElement(By.linkText("Show")).click()
it clicks on the first 'Show' link, which is what I expect.
I can also do:
driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2].1.0")).click()
and that works, as does
driver.findElement(By.xpath("(//a[contains(text(),'Show')])[2]")).click()
So, to sum, all of these work:
driver.findElement(By.linkText("Show")).click()
driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2].1.0")).click()
driver.findElement(By.xpath("(//a[contains(text(),'Show')])[1]")).click()
but this doesn't:
driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/tbody/tr/td[1]")).click()
Why?