【发布时间】:2021-04-05 09:07:13
【问题描述】:
我正在尝试使用 selenium webdriver 定位以下元素:
<div class="lv-product__details"><div class="lv-product__details-head"><span class="lv-product__details-sku">
M40712
</span> <div class="lv-product-add-to-wishlist"><button aria-label="Add to Wishlist" aria-disabled="false" tabindex="0" class="lv-icon-button lv-product-add-to-wishlist__button"><svg focusable="false" aria-hidden="true" class="lv-icon"><use xlink:href="/_nuxt/icons.svg#sprite-navigation-wishlist-off"></use></svg></button></div></div> <h1 class="lv-product__title">
Pochette Accessoires
</h1> <div class="lv-product-variations"><button class="lv-product-variation-selector list-label-l lv-product-variations__selector" aria-expanded="false"><span class="lv-product-variation-selector__title -text-is-medium">
Material
我试过了:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
url = "https://en.louisvuitton.com/eng-nl/products/pochette-accessoires-monogram-005656"
options = Options()
options.headless = True
driver = webdriver.Chrome('path/to/chromedriver', chrome_options=options)
driver.get(url)
elem = driver.find_element_by_class_name("lv-product__details")
或通过 Xpath
elem = driver.find_element_by_xpath('//*[@id="__layout"]/div/div[2]/div[2]/div[1]/div[1]/div[2]')
但 elem 作为空列表返回。我做错了什么/可以做不同的事情来访问网站的内容吗?
【问题讨论】:
-
在没有无头的情况下可以工作吗?如果它确实检查了 page_source,它可能会检测到你的机器人,然后你必须更改选项来修复它,比如添加用户代理等等。
-
@ArundeepChohan 你是对的,只要我将 headless 设置为
True-elem作为空字符串返回。但是,当False时,它能够检索元素。
标签: python html selenium web-scraping