Selenium unable to find element

328 views
Skip to first unread message

Shan

unread,
Sep 12, 2011, 1:23:35 PM9/12/11
to testng-users
Guys,

This has been doing my head in for quite some time so any help with
this will be much appreciated.

I would like to be able to click on a link which when recorded through
the selenium IDE records as 'link=Companies', however i know that in
order to make my script more robust I should change it to an XPATH
equivalent hence I chose the xpath:idrelative option from 'Target'
dropdown list which was as follows:

//table[@id='maincontent']/tbody/tr/td[3]/table[2]/tbody/tr[2]/td/a[2]

When I exported my script as TestNG file, above was transalted as
below:

selenium.click("//table[@id='maincontent']/tbody/tr/td[3]/table[2]/
tbody/tr[2]/td/a[2]");

Now everytime I run my test, Eclips keeps complaning and gives me the
following error:
com.thoughtworks.selenium.SeleniumException: ERROR: Element //
table[@id='maincontent']/tbody/tr/td[3]/table[2]/tbody/tr[2]/td/a[2]
not found

Look foward to your reply.

Cheers

Shan

Jeff

unread,
Sep 12, 2011, 4:24:07 PM9/12/11
to testng...@googlegroups.com
Okay, I'll bite...this has nothing to do with TestNG.
 
That being said the locator "link=Companies" is more robust assuming the text "Companies" is unique to that link on that page.
 
The XPATH "//table[@id='maincontent']/tbody/tr/td[3]/table[2]/tbody/tr[2]/td/a[2]" is highly brittle, particularly if you have dynamic elements in the page.
 
A more robust XPATH would be "//a[.='Companies']" assuming again that "Companies" is a unique value for a link.  A better and more robust way is to target the element by using a unique attribute such as an id or name.
"name=companies_link" or "id=company_link" which in XPATH form would look like:  "//a[@id='company_link']" or "//a[@name='company_link']"
 
You should not assume the code exported from the SeleniumIDE can be run directly and only use it as a template/guide.  SeleniumIDE should only be used as a quick demo tool or debug tool.  Use your brain when determining the most robust location mechanism for a given element.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.




--
I ♥ DropBox !! 

Shan

unread,
Sep 13, 2011, 4:25:08 AM9/13/11
to testng-users
@ Jeff,

Apologies if the question seemed too trivial - I am beginner with
selenium RC therefore even a slightest of errors seems to throw me off
course. I suppose it’s all part of learning curve and I shall continue
to my endeavors in pursuit of better understanding :)

BTW – I’ve managed to sort out the issue – Thanks a lot for you help.
Keep up the good work by replying to these posts as it’s the best
platform for newbies like myself to learn from.

Cheers

Shan
> predato...@gmail.com
> See my LinkedIn profile at:http://www.linkedin.com/in/rjeffreyvincent
> I ♥ DropBox <http://db.tt/9O6LfBX> !!
Reply all
Reply to author
Forward
0 new messages