简体   繁体   English

在 selenium 中找不到任何元素

[英]Can't locate any element in selenium

Please bear with me this is my first real project and I'm trying to code an automated Amazon.com checkout with the help of Python and Selenium.请耐心等待这是我的第一个真正的项目,我正在尝试在 Python 和 Selenium 的帮助下编写一个自动化的 Amazon.com 结帐。

The biggest issue is for now that the webdriver seems unable to locate the "Buy Now" and "Confirm Check Out" button on the defined product page.目前最大的问题是 webdriver 似乎无法在定义的产品页面上找到“立即购买”和“确认结帐”按钮。 I have tried different methods (using XPATH, CSSSELECTOR and NAME) but none are working.我尝试了不同的方法(使用 XPATH、CSSSELECTOR 和 NAME),但没有一个有效。

Anyone got a tip for me?有人给我小费吗? Please find my code above:请在上面找到我的代码:

# 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

Due to page refreshing the first element may not be appearing try using Webdriver waits to allow the page to load.由于页面刷新,第一个元素可能不会出现尝试使用 Webdriver 等待以允许页面加载。

buy_now = driver.find_element_by_css_selector('#buy-now-button')

Replace with用。。。来代替

buy_now=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#buy-now-button")))

Import进口

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM