How to Click Buttons in Python Selenium with Complex HTML Structure

How to perform click button using Selenium in Python? I am new to Python Selenium and need help clicking a button with the following HTML structure:

<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>

I want to click the “Search” and “Reset” buttons individually. I tried the following methods but encountered NoSuchElementException:

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()

How can I correctly locate and click these buttons? Any help is appreciated. Thanks.

With several years of experience in Selenium, I’ve encountered similar issues. To click the ‘Search’ and ‘Reset’ buttons individually, you can use the ActionChains class in Selenium. Here’s how:


from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()

driver.get("https://example.com")

# Locate the "Search" button

search_button = driver.find_element_by_xpath("//div[@onclick=\"submitForm('mTF')\"]")

ActionChains(driver).click(search_button).perform()

# Locate the "Reset" button

reset_button = driver.find_element_by_xpath("//div[@onclick=\"submitForm('rMTF')\"]")

ActionChains(driver).click(reset_button).perform()

This approach should help you correctly locate and click the buttons. For more details on ActionClass in Selenium, check out this guide: What is Actions Class in Selenium

I’ve been working with Selenium for a while, and for a similar issue using PhantomJS as the browser, I approached it this way:


driver.find_element_by_css_selector('div.button.c_button.s_button').click()

By specifying the names of the DIV tags within the selector query, I successfully located and clicked the desired button. If you’re using a different browser like Chrome, the same selector should work fine to click button in Selenium Python.

In my extensive experience debugging Selenium issues, I found this method helpful. To ensure your button click works in Selenium Python, try the following approach:


from selenium.webdriver.support.ui import WebDriverWait

# Save initial page source to file

with open("output_init.txt", "w") as text_file:

text_file.write(driver.page_source.encode('ascii','ignore'))

# Define the XPath of the button you want to click

xpath1 = "your_xpath_here"

destination_page_link = driver.find_element_by_xpath(xpath1)

destination_page_link.click()

# Save destination page source to file after clicking

with open("output_dest.txt", "w") as text_file:

text_file.write(driver.page_source.encode('ascii','ignore'))

# Compare the contents of output_init.txt and output_dest.txt

# If they're the same, your code did not work; if different, your code worked, but there may be other issues.

# Ensure necessary JavaScript transformations are executed before proceeding.

# Option 1: Execute JavaScript and then find the element

# Option 2: Find a comparable hook in output_dest.txt to achieve the desired result

# Option 3: Wait before clicking to ensure elements are loaded

# Example of waiting before clicking using WebDriverWait

xpath2 = "your_xpath_here"

WebDriverWait(driver, timeout=5).until(lambda x: x.find_element_by_xpath(xpath2))

This approach helps verify and debug issues related to clicking buttons in Selenium with Python, ensuring correct functionality and providing troubleshooting steps. The focus on saving and comparing page sources helps in understanding whether the click button Selenium Python operation is successful.