2

I got some problems to find the right element using Selenium. The HTML code looks like this:

<div>
    <div>
        <div>
            <table>
                <thead>
                    <tr>
                        <th></th>
                        <th></th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div></div>
<div>
    <div>
        <div>
            <div>
                <label></label>
                <label></label>
                <select></select>
            </div>
        </div>
    </div>
</div>

(Can’t publish the original URL due its company use only)

In the HTML Code there multiple (count changes every time) Tables looking like the Code above. To find the correct Table I am using this Code:

for index, table in enumerate(self.browser.find_elements(By.XPATH, f'//table/thead/tr/th[{column}]')):
    if table.text == header_text: break

main_table = self.browser.find_elements(By.XPATH, f'//table')[index]

Works fine for me, the problem is that I need to identify the select tag after the correct table as well.
Can anyone help me out with this Code snippet?

0

1 Answer 1

2
self.browser.find_elements(By.XPATH, f'//table/thead/tr/th[{column}]')

this will return a list. you can go to select tag by using the below XPath:

list_of_th = self.browser.find_elements(By.XPATH, f'//table/thead/tr/th[{column}]')
select_web_element = list_of_th[0].find_element(By.XPATH, ".//ancestor::table/../../../following-sibling::div[2]/descendant::select")

should get the job done.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.