[英]Can't locate any element in selenium
请耐心等待这是我的第一个真正的项目,我正在尝试在 Python 和 Selenium 的帮助下编写一个自动化的 Amazon.com 结帐。
目前最大的问题是 webdriver 似乎无法在定义的产品页面上找到“立即购买”和“确认结帐”按钮。 我尝试了不同的方法(使用 XPATH、CSSSELECTOR 和 NAME),但没有一个有效。
有人给我小费吗? 请在上面找到我的代码:
# This count variable is used to check if the product has been bought, if yes it will be 1.
count = 0
page_refreshed = 1
# This while loop is used to check for the "Buy Now" button until it is enabled by Amazon.
while count<= 1:
# This try statement used to click on the "Buy now" Button and click the "Submit Order" Button on the following page
try:
buy_now = driver.find_element_by_css_selector('#buy-now-button')
buy_now.click()
driver.implicitly_wait(8)
buy = driver.find_element_by_css_selector('#bottomSubmitOrderButtonId > span > input')
buy.click()
count += 2
print("Buttons clicked! The item has been bought!");print()
# This except statement used to reload the page every second until the add to cart option is enabled
except:
if count <= 1:
print("Button not appeared, reloading...page reloaded "+str(page_refreshed)+" times!")
driver.refresh()
page_refreshed += 1
pass
由于页面刷新,第一个元素可能不会出现尝试使用 Webdriver 等待以允许页面加载。
buy_now = driver.find_element_by_css_selector('#buy-now-button')
用。。。来代替
buy_now=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#buy-now-button")))
进口
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.