How to Click Buttons with Complex HTML Structure Using Python Selenium?

Patricia Arquette
Release: 2024-10-22 15:58:03
Original
751 people have browsed it

How to Click Buttons with Complex HTML Structure Using Python Selenium?

Python Selenium: Clicking Buttons with Complex HTML Structure

In your encounter with Python Selenium, you're seeking a method to click 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>
Copy after login

Despite attempts using various CSS 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()
Copy after login

You have encountered the NoSuchElementException. To address this challenge, you were intrigued by the possibility of leveraging the onclick attributes of the HTML elements.

The solution lies in the CSS selector you employed. To successfully click the buttons, you need to remove the spaces between the classes in your selector. Here's the corrected version:

driver.find_element_by_css_selector('.button.c_button.s_button').click()
Copy after login

By removing the spaces between the classes, Selenium will accurately identify and click the desired buttons.

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

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!