【问题标题】:Selenium webdriver retrieves an empty listSelenium webdriver 检索一个空列表
【发布时间】:2021-10-04 14:56:12
【问题描述】:

我有这段代码:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions
firefox_options = FirefoxOptions()
firefox_options.add_argument("--headless")
driver = webdriver.Firefox(executable_path = "../../bin/geckodriver", options = firefox_options)

driver.get('https://www.go2roues.com/shop/?category=7&puissance=null&brands=&batterieAmovible=false&connecte=false&livraison=false&capacite=false')

elems = driver.find_elements_by_class_name("pf_pg__product")

print(elems)

从脚本执行此代码时,我得到一个空列表:

[]

当使用相同的 python 二进制文件执行相同的代码时,我的 Python 解释器会得到一个非空的对象列表:

[, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ]

我不知道问题出在哪里。感谢您的帮助

【问题讨论】:

    标签: python-3.x selenium webdriverwait


    【解决方案1】:

    您在获取元素列表之前错过了延迟。
    在访问所需的 Web 元素之前,应完全加载它们。
    让您的代码正常工作的最简单方法是添加一些延迟:

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options as FirefoxOptions
    firefox_options = FirefoxOptions()
    firefox_options.add_argument("--headless")
    driver = webdriver.Firefox(executable_path = "../../bin/geckodriver", options = firefox_options)
    
    driver.get('https://www.go2roues.com/shop/?category=7&puissance=null&brands=&batterieAmovible=false&connecte=false&livraison=false&capacite=false')
    
    time.sleep(10)
    elems = driver.find_elements_by_class_name("pf_pg__product")
    
    print(elems)
    

    为了让它更好,你应该使用如下的显式等待:

    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
    from selenium.webdriver.firefox.options import Options as FirefoxOptions
    firefox_options = FirefoxOptions()
    firefox_options.add_argument("--headless")
    driver = webdriver.Firefox(executable_path = "../../bin/geckodriver", options = firefox_options)
    
    wait = WebDriverWait(driver, 20)
    driver.get('https://www.go2roues.com/shop/?category=7&puissance=null&brands=&batterieAmovible=false&connecte=false&livraison=false&capacite=false')
    
    #wait for the foirst element matching the passed locator presence
    wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".pf_pg__product")))
    
    #add a short delay to make all the elements loaded
    time.sleep(0.6)
    
    #get the elements list
    elems = driver.find_elements_by_class_name("pf_pg__product")
    
    print(elems)
    

    【讨论】:

    • 啊,好吧!我现在明白了。我真笨!这表明,阅读文档很重要。谢谢
    • 不,不仅仅是阅读。当您阅读它时,您并不真正关心和理解,但是当您实际面对该问题时-这就是使您理解的原因。它也被称为“经验”:)
    猜你喜欢
    • 1970-01-01
    • 2020-05-09
    • 2018-01-21
    • 2020-04-21
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多