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

Susan Sarandon
Release: 2024-10-22 12:47:14
Original
930 people have browsed it

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>
Copy after login

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>
Copy after login

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!

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!