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

2 Answers 2

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']"))))
    
Sign up to request clarification or add additional context in comments.

5 Comments

unfortunately didn't help.
@MikołajK Checkout the updated answer and let me know the result
raise exception_class(message, screen, stacktrace, alert_text) selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None Message: Dismissed user prompt dialog: Gotowe? Można sprawdzić?
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ł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.
0

try this

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

1 Comment

or try this self.driver.find_element_by_id('sprawdz').click()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.