Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: can not locate link in table

Tags:

css

selenium

I am writing Selenium script. For a html page include a table, I can not use "css=table tr:nth-child(2) td:nth-child(3) a" to locate the link in the table. Selenium IDE give me the "[error] locator not found".

But use "css=table tr:nth-child(2)", it can locate to the row. So am I mistake for the css locator, I think adding the "td:nth-child(3) a" should work for the link in td , why not?

Edit: I am using firefox 3.0.15

like image 786
zhongshu Avatar asked Nov 13 '09 06:11

zhongshu


People also ask

How to locate table element in Selenium?

Table is located using locator property “tagname”. Using XPath “//*[@id=\”leftcontainer\”]/table/tbody/tr[3]” find the 3rd row and gets its text using getText () function. Using Xpath “//*[@id=\”leftcontainer\”]/table/tbody/tr[3]/td[2]” find the 2nd cell in 3rd row and gets its text using getText () function.

How to check whether link is present or not in Selenium?

To check broken links in Selenium, the process is simple. On a web page, hyperlinks are implemented using the HTML Anchor (<a>) tag. All the script needs to do is to locate every anchor tag on a web page, get the corresponding URLs, and run through the links to check if any of them are broken.

How to find XPath from table?

Let's select an element in the web table and find its XPath. For Chrome, right-click and inspect the given element to find its XPath. To find the XPath of a UI element in Firefox, right-click on the desired element, go to “Inspect Element” to open the inspector which will help identify its XPath.


1 Answers

Given the HTML:

<html>
  <body>
    <table>
      <tr><td>Hello</td><td>World</td></tr>
      <tr><td>I'm</td><td><a href="http://www.example.com/">Batman</a></td></tr>
    </table>
  </body>
</html>

You can use the following locator for the link in the 2nd column of the 2nd row:

css=tr:nth-child(2) > td:nth-child(2) > a

Update:

After a little bit of research, it seems your original locator should work, but doesn't due to a bug in the cssQuery library used by Selenium (http://jira.openqa.org/browse/SEL-698). My suggestion above works, but it's really only a workaround until the bug is fixed. Unfortunately, considering the cssQuery hasn't been updated for some time I'm not sure how soon this will be addressed.

like image 153
Dave Hunt Avatar answered Nov 15 '22 09:11

Dave Hunt