【问题标题】:Selenium is unable to locate elements by class nameSelenium 无法按类名定位元素
【发布时间】:2020-06-21 16:39:44
【问题描述】:

我正在尝试从this page 获取价格列表。

我要获取的元素的类名称为s-item__price。 这是我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

url = 'https://www.ebay.de/sch/i.html?_from=R40&_nkw=iphone+8+&_sacat=0&LH_TitleDesc=0&LH_ItemCondition=3000&rt=nc&LH_Sold=1&LH_Complete=1'

chrome_options = Options()
chrome_options.add_argument('--headless')

browser = webdriver.Chrome(options=chrome_options)

browser.get(url)

print(browser.find_elements_by_class_name('s-item__price'))

browser.quit()

输出只是一个空列表。

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping


    【解决方案1】:

    您可以使用 WebDriverWait 等到 javascript 生成元素:

    wait = WebDriverWait(browser, 15) # 15 sec timeout
    wait.until(expected_conditions.visibility_of_element_located((By.CLASS_NAME, 's-item__price')))
    

    您也可以使用presence_of_elements_located,但如果涉及到点击交互,则它不适用于隐藏元素。 所以更喜欢使用:visibility_of_element_located

    示例代码:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions
    
    url = 'https://www.ebay.de/sch/i.html?_from=R40&_nkw=iphone+8+&_sacat=0&LH_TitleDesc=0&LH_ItemCondition=3000&rt=nc&LH_Sold=1&LH_Complete=1'
    
    options = Options()
    options.add_argument('--headless')
    
    browser = webdriver.Chrome(options=options)
    browser.get(url)
    
    wait = WebDriverWait(browser, 15) # Throws a TimeoutException after 15 seconds
    wait.until(expected_conditions.visibility_of_element_located((By.CLASS_NAME, 's-item__price')))
    # you may also culd use the return value of the wait
    
    print(browser.find_elements_by_class_name('s-item__price'))
    browser.quit()
    

    【讨论】:

    • 此方法适用于其他元素,但由于某种原因不适用于s-item__price。我什至尝试将超时时间增加到 1 分钟,但在设定的时间之后我一直抛出超时。
    • 尝试print(browser.find_elements_by_class_name('s-item__price')[0].text)从第一个元素中获取文本
    • 如果你想用 ebay 做一些更复杂的事情。你也可以试试 ebay api:developer.ebay.com
    【解决方案2】:

    我认为你得到一个空列表是因为你需要等待。

    使用 WebDriverWait 并使用 .presence_of_all_elements_located 来收集列表中的元素。

    然后用循环提取它们,你必须调用.text方法来抓取文本

    browser.get('https://www.ebay.de/sch/i.html?_from=R40&_nkw=iphone%208%20&_sacat=0&LH_TitleDesc=0&LH_ItemCondition=3000&rt=nc&LH_Sold=1&LH_Complete=1')
    wait = WebDriverWait(browser, 20)
    list_price = wait.until(EC.presence_of_all_elements_located((By.CLASS_NAME, 's-item__price')))
    for price in list_price:
        print(price.text)
    driver.quit()
    

    导入后:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 2020-03-05
      • 2015-11-08
      • 2020-05-19
      • 2022-01-06
      相关资源
      最近更新 更多