3030tank
3030tank

Reputation: 109

Issue clicking Javascript button with python/Selenium

I am trying to click the button labled "Pickup" at the following link :

https://firehouse.alohaorderonline.com/StartOrder.aspx?SelectMenuType=Retail&SelectMenu=1000560&SelectSite=01291

My code is below but does nothing until it fails with the error

element not interactable

pickupurl = 'https://firehouse.alohaorderonline.com/StartOrder.aspx?SelectMenuType=Retail&SelectMenu=1000560&SelectSite=1291'

driver = webdriver.Chrome('d:\\chromedriver\\chromedriver.exe')
driver.implicitly_wait(80)
driver.get(pickupurl)



button = driver.find_elements_by_xpath('//*[@id="ctl00_ctl00_PickupButton"]')
button.click()

The code appears to locate an element as when I print 'button' I get an element object.

I've tried using driver.execute_script to execute the onclick= attribute but this does nothing as well.

Any help is appreciated.

Upvotes: 5

Views: 962

Answers (2)

Moshe Slavin
Moshe Slavin

Reputation: 5204

Using WebDriverWait and expected_conditions is a good practice!

see explicit-waits.

This works for me:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

pickupurl = 'https://firehouse.alohaorderonline.com/StartOrder.aspx?SelectMenuType=Retail&SelectMenu=1000560&SelectSite=1291'
driver = webdriver.Chrome('d:\\chromedriver\\chromedriver.exe')
driver.get(pickupurl)
wait = WebDriverWait(driver, 10)
pickup_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@id='btnPickupDiv']/div[@class='Button']")))

pickup_button.click()
loacter = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "AddressZipLabelDiv")))
driver.quit()

The issue you had probably had something to do with find_elements_by_xpath you should use find_element_by_xpath without the s...

Upvotes: 4

QHarr
QHarr

Reputation: 84465

The following works for me

from selenium import webdriver

d = webdriver.Chrome()
url = 'https://firehouse.alohaorderonline.com/StartOrder.aspx?SelectMenuType=Retail&SelectMenu=1000560&SelectSite=01291'
d.get(url)
d.execute_script('document.getElementById("ctl00_ctl00_Content_Content_btnPickup").click();')

Helpful warning from @Andersson

element.click() executed via execute_script doesn't really makes a click, but just triggers onclick action of element (link, input, button, etc). So if you're using this approach for web-testing you might miss tons of bugs

Upvotes: 3

Related Questions