0

So I just started using Selenium for Python and I am trying to click on a button element that is buried into a few div elements; I've tried so many things but nothing works. Everything in the code works besides the last part which is waiting for the button to be clickable and then clicking it. I would greatly appreciate some help here, thanks. :)

HTML:

html code

Code trials:

my python code

Error stacktrace:

my python code error

4
  • 1
    Please, consider inserting your code as text, not pictures, so that anyone could copy it to try. Commented Dec 27, 2019 at 18:54
  • Look at this github.com/mindhashnl/roomsignage/blob/development/mysign_app/…. This is what we used Commented Dec 27, 2019 at 18:55
  • Never mind, I now see it's not your html and thus you cannot add name or id Commented Dec 27, 2019 at 19:04
  • But the find_element_by_XPath might still be viable (link in previous comment) Commented Dec 27, 2019 at 19:05

3 Answers 3

3

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

Comments

1

css selectors will become your best friend,

you should always look to add as many attributes as possible

maybe_later_css = 'button[class="btn btn-danger"]'
# type str, '<tag-name>[<attribute-name> = <attribute-value>]'

driver.find_element_by_css_selector(maybe_later_css).click()

follow this format for all elements, its superior and works as expected every time

the only complication is when there exists multiple buttons with them same class name, in which case you should find a different attribute to fill the [] brackets

Comments

1

The element with text as Maybe Later is within a Modal Dialog Box so to locate and click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.modal-footer#eFooter button.btn.btn-danger.x[style='danger']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='modal-footer' and @id='eFooter']//button[@class='btn btn-danger x' and @style='danger']"))).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
    

Comments

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.