Selenium无法通过类名称找到元素

时间:2020-06-21 16:39:44

标签: python selenium selenium-webdriver web-scraping

我正在尝试从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()

输出只是一个空列表。

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

您会得到一个空列表,因为您需要等待。

使用 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
相关问题