selenium doesn't find object identified by absolute xpath

302 views
Skip to first unread message

Davina Armstrong

unread,
Jan 15, 2010, 1:24:50 PM1/15/10
to Selenium Users
I'm trying to automate clicking on a specific cell in a table. In my
app, this action highlights a row. Selenium IDE doesn't capture the
click, so I inspected the element using Firebug. I used it's "Copy
Xpath" function, which gave me:

/html/body/div[4]/table/tbody/tr[2]/td/div/table/tbody/tr[2]/td/div/div
[5]/div/table/tbody/tr[2]/td/div/div/div/div/div/table/tbody/tr/td/
table/tbody/tr/td/div/div/div[2]/table/tbody/tr[4]/td[3]

When I call selenium.click("/html/body/div[4]/table/tbody/tr[2]/td/div/
table/tbody/tr[2]/td/div/div[5]/div/table/tbody/tr[2]/td/div/div/div/
div/div/table/tbody/tr/td/table/tbody/tr/td/div/div/div[2]/table/tbody/
tr[4]/td[3]"), I get an object not found error.

Can anyone help me figure out how I can successfully click on this
table cell?

Thanks,
Davina

Santiago Suarez Ordoñez

unread,
Jan 17, 2010, 7:02:53 PM1/17/10
to seleniu...@googlegroups.com
There's no way we could find an error just by looking at that xpath. If the xpath comes from firebug, the syntax is correct, so the problem could be the timing (element is still not present when the assert is made) or who knows.
The only thing I can help with, is saying that such a long xpath is terribly brittle, and will make your tests fail every time a dev changes the application.
To write better xpaths (or even better, css), you should start looking at class and id attributes, to change that huge and horribly 3 lines string into something like:

//table[@id="myTable"]//td[@class="interestingCell"]

Hope it helps.

Santi


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




Meerasaaheb Mohmmad

unread,
Jan 18, 2010, 12:25:37 AM1/18/10
to Selenium Users
HI Davina,

Open that webpage and open selenium IDE.

In Target u enter that entire XPATH and click the Find button and
check whether the link or field u want is highlighted or not..
If it is highlighted the XPATH is correct if not it is not correct....
Do u have ant text in td[3]...If u provide so it would be help to find
out another means ...

Thanks,
Meerasaaheb Mohmmad

darrell

unread,
Jan 18, 2010, 12:40:36 PM1/18/10
to Selenium Users
Davina,

As Santiago has pointed out, if you copied it from Firebug it is most
likely the correct xpath. What happened before you do the click is
probably the problem. For example, if the code is:

// login
// click the cell in the table

You might want to change it to:

// login
// wait for page to load
// click the cell in the table

Anytime a new page loads you should always wait for the page to finish
loading. Additionally, if you are using javascript and an action on
the current page updates the DOM (onchange or onclick events), you
need some code to wait for the change to occur. For example, I have a
select list of companies. When you select a company, AJAX code updates
a second select list with the employees of that company. It can take a
few seconds if the company has a lot of employees. I needed the
developer to add in something to flag when the change was finished. I
then wrote my own waitForEmployeesToLoad() method based on that flag.

Darrell

ds tara

unread,
Jan 18, 2010, 12:47:00 PM1/18/10
to seleniu...@googlegroups.com
Meerassaaheb's suggestion was helpful:  I grabbed the xpath from Firebug and asked Selenium IDE to find it, which it couldn't.  Does anyone know what could account for this discrepancy?

Thanks for the other suggestions.  I've verified that the page has finished loading (I, too, had to have code added into the app itself to inform me when the ajax actions had completed).

If I wanted to accomplish this by finding the cell that has text="foo", how would I accomplish that?

Thanks,
Davina

darrell

unread,
Jan 19, 2010, 3:46:44 PM1/19/10
to Selenium Users
It is not as common with Selenium as other test tools but sometimes
whitespace can make a difference. When you look at something like
id="foo" in Firebug it can be id=" foo " in the source code. For this
reason, if I was looking for a cell which contains the text "foo" I
would use:

"xpath=//TD[contains(text(),'foo')]"

rather than something like:

"text=foo"

Additionally, if the cell is actually defined as:

<td><span class="something">foo</span></td>

then the text is really part of the SPAN and not part of the cell. I'd
use:

"xpath=//SPAN[contains(text(),'foo')]/.."

to find the cell.

> > selenium-user...@googlegroups.com<selenium-users%2Bunsubscribe@go oglegroups.com>

Belter

unread,
Jan 25, 2010, 4:18:52 AM1/25/10
to Selenium Users
To get a 'better' XPath try using the XPath Checker plug in for
firefox.

This will try to reduce the length of the XPath reference and seems to
be quite good at getting unique paths referenced to an id.

You will have to check the syntax, for example, I just captured an
XPath using XPath Checker and it gave me:

id('content')/table/tbody/tr[3]/td/table/tbody/tr[2]/td/img

Selenium needs:

//img[@id='content']/table/tbody/tr[3]/td/table/tbody/tr[2]/td/img

or the //img may need to be //td or //tr or //table, it depends on the
html. The good thing about XPath Checker is that it will interpret
the selenium format XPaths so you can put the edited version back in
and check it finds it.

I have seen situations in the past where the html in IE is subtly
different to the html in firefox so having an id referenced XPath will
reduce the chance of this being a problem compared to the full html
path from the root of the page.

Alternatively, if you look at the <td> element at the end of the chain
with firebug and it has some content that can identify it uniquely you
could try that. For instance if it has a href="texttextsomething
uniquetexttext" attribute you could try:

//td[contains(@href,'something unique')]

HTH

David Lambert

nellore krishna

unread,
Jan 25, 2010, 6:42:30 AM1/25/10
to seleniu...@googlegroups.com
Hi Davina,

        Try to reduce XPATH.

        Or

        /*
               Below command will wait till Element[Xpath ... Has to be provided here] is loaded in page.
       */      

        for (int second = 0;; second++) {
            if (second >= 60) fail("timeout");
            try { if (selenium.isElementPresent("Xpath ... Has to be provided here")) break; } catch (Exception e) {}
            Thread.sleep(1000);
        }



Thanks,
Nellore Krishna Kumar

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.




--
            Nellore Krishna kumar
Reply all
Reply to author
Forward
0 new messages