Home >Backend Development >Python Tutorial >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>
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()
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()
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!