user1636922
user1636922

Reputation: 2569

Selenium doesn't always grab correct dynamically generated link

My current environment is Firefox 3.6.28 using Selenium RC (with the Selinium server 2). Eventually things will be migrated to Selenium 2.0 with the WebDriver stuff, but I have some tests I need to get working in Selenium RC at the moment.

I have a dynamically generated table which has an "a href" link inside each row. I want to click on the link on a certain row. However, the problem is sometimes Selenium correctly picks it out (test passes), and sometimes I get an error saying that the element doesn't exist (test fails):

[testng] com.thoughtworks.selenium.SeleniumException: ERROR: Element xpath=(//table[@class='results'])/tbody/tr[position()=5]/td/a not found

There does not seem to be any pattern for detecting/failing to see the element.

To see if the browser actually sees the element when I get the error, I have something like this:



    ...
    try{
        wrapWaitForPageToLoad("10000");
        wrapWaitForPageToLoad("10000");
        clickAndWait(xpath);
    }catch(Exception e) { //loop indefinitely here }

And it turns out that the browser sees the element. I test out the xpath in the Selenium IDE. The xpath is clearly correct since I could execute the click command with that xpath in the Selenium IDE.

In case some elements didn't fully load, I had added in some wrapWaitForPageToLoad() with value 10000. However, this does not seem to have an effect on the problem. And in any case, the elements all seem to be fully loaded as the above tests would suggest... Also increasing the time doesn't help.

The partial html that firebug gives me on the table:
(Note, position()=5 to get listdata4 because the first row is the table's title)

<table class="results" cellspacing="0" cellpadding="1" border="0" style="cursor: default;">
<tbody>
<tr id="titles">
<tr class="listdata1" style="">
<tr class="listdata2" style="">
<tr class="listdata3" style="">
<tr class="listdata4" style="">
    <td align="center">...</td>
    <td>
        <a href="/click/this/link">Cake Pictures</a>
    </td>

What could be causing this odd error?

Upvotes: 2

Views: 928

Answers (1)

Roman C
Roman C

Reputation: 1

You need to modify xpath from

//table[@class='results'])/tbody/tr[position()=5]/td/a

to

//table[@class='results']/tbody/tr[5]/td[2]/a

Upvotes: 1

Related Questions