[英]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.