Anon0441
Anon0441

Reputation: 67

Trouble finding button

I am having trouble with selenium not wanting to find a button. The code I used worked on previous buttons on the same website, but for some reason it is having trouble finding this one.

Here is the HTML:

<button id="getCoupon" class="getCoupon" onclick="IWant()" style="" data-i18n="view_prod_get_coupon">Get Your Coupon</button>

This is what I have tried so far:

driver.find_element_by_id('getCoupon').click()
driver.find_element_by_xpath('//*[@id="getCoupon"]').click()
driver.find_element_by_class_name('getCoupon').click()

Here are the errors for the last two: Trying with XPath:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="getCoupon"]"}

Trying with class name:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"getCoupon"}

Upvotes: 0

Views: 62

Answers (2)

NarendraR
NarendraR

Reputation: 7708

Your locator seems fine, Probably execution happening and fast as your element not getting load on time. You can try using explicit waits

// wait for element present 

element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "getCoupon"))
element.click()

// wait until element get visible

element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "getCoupon"))
element.click()

For debugging purpose you can also use a method like

element = driver.find_element_by_id('getCoupon')

if element.is_displayed():
    element.click()
else:
    print ("element not visible ")

don't forget to import required packages. Refer this for more.

Upvotes: 1

Vasyl Kushnir
Vasyl Kushnir

Reputation: 76

Try to look into html code. I think it can be:

  1. This button may be in frame then you need to switch to frame first and then to locate button.

  2. Maybe it javascript generated, so you need to wait until it will be generated by js code.

Upvotes: 0

Related Questions