franklin
franklin

Reputation: 1819

How do I click on a dynamically loaded link?

I am currently working on web automation via Selenium.

I have a html file where the relevant part is this:

<table>
    <tbody>
        <tr>
            <td class="tabon" nowrap="">
                <div class="tabon">
                    <a id="tab" href="(long dynamically generated string)">
                        <b>Main Page</b>
                    </a>
                </div>
            </td>
            <td class="taboff" nowrap="">
                <div class="taboff">
                    <a id="tab" href="(another long string)">Info</a>
                </div>
            </td>
        </tr>
    </tbody>
</table>

I want to be able to access the second tab. Using Selenium I can't actually "click" on the div tag.

    try:
        browser.find_element_by_xpath(
            '//table/tbody/tr/td[2]/div/a').click()
    except NoSuchElementException:
        print ('error')

This always results in an error. It has something to do with the fact that when the div tag is interacted with, it clicks on the URL anchor which changes the div such that the clicked on tag has a "tabon" property. How can Selenium mimic this?

EDIT: I neglected to note that the class with "tabon" has the title of the page in a separate bold tag.

Upvotes: 1

Views: 489

Answers (2)

Subh
Subh

Reputation: 4424

Try this code, in case the tab "My Info" is visible on the webpage:

browser.find_element_by_xpath("//a[.='My Info']").click()

This will click on the element with tag 'a' and having exact innerHTML/text as My Info.

Upvotes: 1

Saifur
Saifur

Reputation: 16201

You need to be passing click on a tag not on div and in addition to the solution Subh provided you can use .taboff a as cssselector. This selector walks you down to a tag from second td of pasted html

Upvotes: 0

Related Questions