Home  >  Article  >  Backend Development  >  How to Click Complex Buttons with Nested HTML Structures Using Python Selenium?

How to Click Complex Buttons with Nested HTML Structures Using Python Selenium?

Susan Sarandon
Susan SarandonOriginal
2024-10-22 12:47:14926browse

How to Click Complex Buttons with Nested HTML Structures Using Python Selenium?

Python Selenium: Clicking Complex Buttons

Challenge: Click on buttons with complex HTML structures in Python Selenium

Question:

A user encountered challenges locating and clicking buttons with the following HTML structure:

<code class="html"><div class="b_div">
    <div class="button c_button s_button" onclick="submitForm('mTF')">
        <input class="very_small" type="button">
        <div class="s_image"></div>
        <span>Search</span>
    </div>
    <div class="button c_button s_button" onclick="submitForm('rMTF')" style="margin-bottom: 30px;">
        <input class="v_small" type="button">
        <span>Reset</span>
    </div>
</div></code>

Attempted Solutions:

The user tried using various Selenium selectors, such as:

  • driver.find_element_by_css_selector('.button .c_button .s_button').click()
  • driver.find_element_by_name('s_image').click()
  • driver.find_element_by_class_name('s_image').click()

However, they received NoSuchElementException errors.

Solution:

To fix the issue, the user needed to remove spaces between class names in the CSS selector. The correct selectors are:

<code class="python">driver.find_element_by_css_selector('.button.c_button.s_button').click()</code>

The above is the detailed content of How to Click Complex Buttons with Nested HTML Structures Using Python Selenium?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn