drake10k
drake10k

Reputation: 437

Selenium click dynamic link

I have a page structure similar to this:

<html>
    <head/>
    <frameset>
        <frame/>
        <frameset id="id1">
            <frame/>
            <frame id="id2">
                <html>
                    <head/>
                    <body class="class1">
                        <form id="id3">
                            <input/>
                            <input/>
                            <input/>
                            <input/>
                            <table/>
                            <table/>
                            <table/>
                            <div id="id4">
                                <div id="id5">
                                    <table id="id6">
                                        <thead/>
                                        <tbody>
                                            <tr/>
                                            <tr/>
                                            <tr/>
                                            <tr>
                                                <td/>
                                                <td/>
                                                <td>
                                                    <a href="dynamic link">Text</a>

I need to click on the dynamic link - the link and position inside the table varies, but the text is always the same.

I've tried using find_element_by_link_text and it fails.

Using xpath it can not find the form element.

Thank you.

Upvotes: 0

Views: 1931

Answers (2)

JimEvans
JimEvans

Reputation: 27496

You need to switch to the frame containing the <a> element first. Your code would look something like this:

driver.switch_to_frame('id3')
driver.find_element_by_link_text('TEXT').click()

Note that the above code is only an approximation, since your provided HTML code is only an approximation. In particular, you have a <frameset> element as a direct child of another <frameset> element, which I believe is invalid HTML. If you indeed have nested framesets, you'll need multiple calls to switch_to_frame to navigate down the frame hierarchy until your focus is on the frame containing the document with the element you're looking for.

Upvotes: 2

Ishaan
Ishaan

Reputation: 886

You can first find all a tags in the page using: find_elements_by_tag_name

Then iterate over each a tag and check its text since text is always the same

a_tags = driver.find_elements_by_tag_name('a')
for a in a_tags:
    if a.text == 'TEXT':
        a.click()

Upvotes: 2

Related Questions