Python Selenium Click on Button

python selenium click on button

For python, use the

from selenium.webdriver import ActionChains

and

ActionChains(browser).click(element).perform()

How do I make Selenium Python click on a button element?

To click on **Maybe Later** button.
Induce WebDriverWait() and element_to_be_clickable() and following XPATH or CSS Selector.

XPATH:

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='modal-footer']//button[@Class='btn btn-danger x' and text()='Maybe Later']"))).click()

CSS Selector:

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.modal-footer button.btn.btn-danger.x[style='danger']"))).click()

You need to import following libraries.

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

Python/Selenium: Click on button element without ID attribute

There is no such method as send_mouse(Keys.LEFT)


To click() on the element with text as Accept All you can use either of the following locator strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, "button[tabindex='0']").click()
  • Using xpath:

    driver.find_element(By.XPATH, "//button[text()='Accept All']").click()

xpath for click to a button - python/selenium

You probably missing a delay / wait.

After clicking the submit / login button on the login page it takes some time to make the internal page loaded.

The recommended way to do that is to use Expected Conditions explicit waits, something like this:

wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='advanced']"))).click()

To use the wait object here you will need to import the following inports:

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

And to initialize the wait object with

wait = WebDriverWait(driver, 20)

You will have to validate your locator is unique. As well as to validate the element you are trying to access is not inside iframe and not in a new window / tab etc.

How to click on Search button using Python Selenium

To click on the element Suchen you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.jameda.de/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[class^='SearchString']"))).click()
  • Using XPATH:

    driver.get('https://www.jameda.de/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Suchen')]"))).click()
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
  • Browser Snapshot:

jameda



Related Topics



Leave a reply



Submit