Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get Selenium Button Click to work?

I’m a complete beginner, I have a website in which I want to click a button through python selenium, I’ve tried a lot of things, but I can’t get it to work.

import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


chromedriver_location = executable_path=r"C:\Users\HP\Desktop\chromedriver_win32\chromedriver.exe"

driver = webdriver.Chrome(chromedriver_location)
driver.get('https://maskun.org/donate/')

donate_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'give-btn advance-btn')] and text()='Donate Now']")))
donate_button.click()

Right now, I’m getting the error:

Traceback (most recent call last):
  File "d:\pythonprojects\maskun.py", line 13, in <module>
    donate_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(@class, 'give-btn advance-btn')] and text()='Donate Now']")))
  File "C:\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 81, in until
    value = method(self._driver)
  File "C:\Python\Python310\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 312, in _predicate
    target = driver.find_element(*target)  # grab element at locator
  File "C:\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 856, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute
    self.error_handler.check_response(response)
  File "C:\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //button[contains(@class, 'give-btn advance-btn')] and text()='Donate Now'] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//button[contains(@class, 'give-btn advance-btn')] and text()='Donate Now']' is not a valid XPath expression.      
  (Session info: chrome=106.0.5249.119)
Stacktrace:
Backtrace:
        Ordinal0 [0x00B51ED3+2236115]
        Ordinal0 [0x00AE92F1+1807089]
        Ordinal0 [0x009F66FD+812797]
        Ordinal0 [0x009F92B4+823988]
        Ordinal0 [0x009F9165+823653]
        Ordinal0 [0x009F9400+824320]
        Ordinal0 [0x00A25352+1004370]
        Ordinal0 [0x00A257CB+1005515]
        Ordinal0 [0x00A57632+1209906]
        Ordinal0 [0x00A41AD4+1120980]
        Ordinal0 [0x00A559E2+1202658]
        Ordinal0 [0x00A418A6+1120422]
        Ordinal0 [0x00A1A73D+960317]
        Ordinal0 [0x00A1B71F+964383]
        GetHandleVerifier [0x00DFE7E2+2743074]
        GetHandleVerifier [0x00DF08D4+2685972]
        GetHandleVerifier [0x00BE2BAA+532202]
        GetHandleVerifier [0x00BE1990+527568]
        Ordinal0 [0x00AF080C+1837068]
        Ordinal0 [0x00AF4CD8+1854680]
        Ordinal0 [0x00AF4DC5+1854917]
        Ordinal0 [0x00AFED64+1895780]
        BaseThreadInitThunk [0x76046739+25]
        RtlGetFullPathName_UEx [0x77858FD2+1218]
        RtlGetFullPathName_UEx [0x77858F9D+1165]

If I inspect the button on chrome I get:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<button class="give-btn advance-btn" tabindex="1">Donate Now<i class="fas fa-chevron-right"></i></button>

The XPath is:

//*[@id="give-form-746-1"]/div[1]/button

>Solution :

you need to switch to frame first than find_elemnt

driver.get('https://maskun.org/donate/')
iframe = driver.find_element(By.XPATH, "//iframe[@name='give-embed-form']")
driver.switch_to.frame(iframe)
donate_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Donate Now')]")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading