0

Can you tell me why selenium can't click a button. I tried xpath, id, class, text and nothing. i get info that there is no such element or sth like that but in firefox i can see that there's an item the name is the same. No idea whats wrong.

self.driver.execute_script("window.scrollTo(0, 3500)")
sleep(1)
#self.action.move_to_element(przycisk).click(sprawdz).perform()
self.driver.find_element_by_xpath("//button[@id='sprawdz']").click();
#self.driver.find_element_by_link_text("ok").click();

button on the website

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Mikołaj K
  • 35
  • 4

2 Answers2

1

To click on the element you can use either of the following Locator Strategies:

  • Using css_selector:

    self.driver.find_element_by_css_selector("button.btn.btn-large#sprawdz").click()
    
  • Using xpath:

    self.driver.find_element_by_xpath("//button[@class='btn btn-large' and @id='sprawdz']").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-large#sprawdz"))).click()
    
  • Using XPATH:

    WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-large' and @id='sprawdz']"))).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
    

Update

As a last resort you can use execute_script() method as follows:

  • Using CSS_SELECTOR:

    driver.execute_script("arguments[0].click();", WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-large#sprawdz"))))
    
  • Using XPATH:

    driver.execute_script("arguments[0].click();", WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-large' and @id='sprawdz']"))))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • unfortunately didn't help. – Mikołaj K Dec 21 '20 at 21:35
  • @MikołajK Checkout the updated answer and let me know the result – undetected Selenium Dec 21 '20 at 21:42
  • raise exception_class(message, screen, stacktrace, alert_text) selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None Message: Dismissed user prompt dialog: Gotowe? Można sprawdzić? – Mikołaj K Dec 21 '20 at 21:48
  • this Message: Dismissed user prompt dialog: Gotowe? Można sprawdzić? is a pop up/ alert after clicking the button. but the script doesn't click it. I can only manually click the button and then i see an alert asking if i'm sure i want to check. Maybe this alert is a problem – Mikołaj K Dec 21 '20 at 21:48
  • @MikołajK This solution was to click on the button with text as **SPRAWDZ** nothing more or less. We aren't sure about the **pop up** / **alert**. Feel free to raise a new question as per your new requirement? Stackoverflow contributors will be happy to help you out. – undetected Selenium Dec 21 '20 at 22:25
0

try this

element = self.driver.find_element_by_xpath("//button[@id='sprawdz']")
self.driver.execute_script("arguments[0].click();", element)
jaeyoung
  • 13
  • 2