Selenium无法按类名定位元素

2024-06-07 08:25:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从this page那里得到一份价格表

我试图获取的元素的类名称为s-item\u 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()

输出只是一个空列表


Tags: fromimportbrowserurlseleniumpagechromethis
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

您可以使用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()

相关问题 更多 >

    热门问题