Note that there are some explanatory texts on larger screens.

plurals
  1. POClicking on a hypertext link using XPath
    text
    copied!<p>Apologies if this is a dumb question - I'm new to Selenium.</p> <p>I have a web page I'm testing that has a few hypertext links in a table. The HTML looks like this:</p> <pre><code>&lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;td&gt;&lt;b&gt;History&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Attributes&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Xml&lt;/b&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;a href=link here&gt;Show&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;a href=link here&gt;Show&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;a href=link here&gt;Show&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>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:</p> <pre><code>driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/thead/tr/td[1]").getText() </code></pre> <p>correctly returns 'History' and</p> <pre><code>driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/tbody/tr/td[1]").getText() </code></pre> <p>correct returns 'Show'.</p> <p>So I would think that:</p> <pre><code>driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/tbody/tr/td[1]")).click() </code></pre> <p>would click on the 'Show' link in the first column. But it doesn't - nothing happens.</p> <p>If I do:</p> <pre><code>driver.findElement(By.linkText("Show")).click() </code></pre> <p>it clicks on the first 'Show' link, which is what I expect.</p> <p>I can also do:</p> <pre><code>driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2].1.0")).click() </code></pre> <p>and that works, as does</p> <pre><code>driver.findElement(By.xpath("(//a[contains(text(),'Show')])[2]")).click() </code></pre> <p>So, to sum, all of these work:</p> <pre><code>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() </code></pre> <p>but this doesn't:</p> <pre><code>driver.findElement(By.xpath("//div[@id='content']/div/form/div/table[2]/tbody/tr/td[1]")).click() </code></pre> <p>Why?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload