使用python/Selenium单击Javascript按钮的问题

2024-06-25 22:54:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试单击以下链接中标记为“Pickup”的按钮:

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

我的代码在下面,但在出现错误之前不会执行任何操作

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()

当我打印“button”得到一个element对象时,代码似乎定位了一个元素。你知道吗

我试过用driver.execute\u脚本执行onclick=属性,但这也不起作用。你知道吗

感谢您的帮助。你知道吗


Tags: 代码httpscomdriverbuttonelementretailselectmenu
2条回答

以下是我的作品

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();')

来自@Andersson的有用警告

元素。单击()通过execute_脚本执行并没有真正的点击,只是触发元素的onclick操作(链接、输入、按钮等)。因此,如果您使用这种方法进行web测试,您可能会漏掉大量错误

使用^{}^{}是一个很好的实践!你知道吗

explicit-waits。你知道吗

这对我很有用:

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()

问题可能与find_elements_by_xpath有关,您应该使用find_element_by_xpath,而不使用s。。。你知道吗

相关问题 更多 >