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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Darian Moore
  • 33
  • 1
  • 6
  • 1
    Please, consider inserting your code as text, not pictures, so that anyone could copy it to try. – MasterAler Dec 27 '19 at 18:54
  • Look at this https://github.com/mindhashnl/roomsignage/blob/development/mysign_app/tests/frontend/admin/test_add_user.py. This is what we used – MartW Dec 27 '19 at 18:55
  • Never mind, I now see it's not your html and thus you cannot add name or id – MartW Dec 27 '19 at 19:04
  • But the find_element_by_XPath might still be viable (link in previous comment) – MartW Dec 27 '19 at 19:05

3 Answers3

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
KunduK
  • 32,888
  • 5
  • 17
  • 41
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

rnath
  • 146
  • 5
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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352