Css 使用Selenium web驱动程序获取产品名称时出现InvalidSelector错误
我试图在电子商务页面上获取产品的名称和价格。我正在使用Selenium,我的代码是:Css 使用Selenium web驱动程序获取产品名称时出现InvalidSelector错误,css,selenium,xpath,web-scraping,scrapy,Css,Selenium,Xpath,Web Scraping,Scrapy,我试图在电子商务页面上获取产品的名称和价格。我正在使用Selenium,我的代码是: for element in WebDriverWait(self.driver, 30).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, '.product-iWrap'))): product_name_tmall = element.find_element_by_css_selector(
for element in WebDriverWait(self.driver, 30).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, '.product-iWrap'))):
product_name_tmall = element.find_element_by_css_selector('.productTitle a')
product_price_tmall = element.find_element_by_css_selector('.productPrice em::text')
tmallSpider.items['product_name_tmall'] = product_name_tmall
tmallSpider.items['product_price_tmall'] = product_price_tmall
yield tmallSpider.items
当我运行时,它会给我这个错误
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
我之前使用了相同css选择器的scrapy,它生成了正确的信息:
product_info = response.css('.product-iWrap')
for product in product_info:
product_name_tmall = product.css('.productTitle a').xpath('normalize-space(.)').get()
product_price_tmall = product.css('.productPrice em::text').extract()
tmallSpider.items['product_name_tmall'] = product_name_tmall
tmallSpider.items['product_price_tmall'] = product_price_tmall
product_detail_link = 'http:' + product.css('a::attr(href)')[0].extract()
yield scrapy.Request(product_detail_link, callback=self.start_scraping)
我不知道为什么css路径在Selenium中不起作用。
页面的HTML为:
<div class="product-iWrap">
<p class="productPrice">
<em title="6599" data=spm-anchor-id="a220m.1000858.100725 ..." class>...</em>
</p>
<p class="productTitle">
<a href="//detail.tmall..." target="blank" title="iPad Air 3"...>...</a>
</p>
</div>
...
对于这个产品,我想得到6599和iPad Air 3,我想在第一页上看到所有产品的信息。知道怎么做吗?
这是页面的URL:您试图使用无效的CSS选择器。
::text
伪选择器不是任何实际CSS规范的一部分。有可能,甚至有可能,Scrapy支持不属于CSS标准的选择器。jQuery也曾这样做,非标准选择器支持对Selenium用户造成了不小的困惑,因为Selenium只支持浏览器本机选择器引擎支持的CSS部分
另外,如果您使用的是CSS选择器,但不确定它是否正确,则可以打开浏览器的开发人员工具,并转到其JavaScript控制台。键入document.querySelector(“”)
并按enter键。如果语句返回一个元素,您应该能够在Selenium代码中使用选择器。否则,您将无法成功使用它
在这种特定情况下,我将执行以下操作:
for element in WebDriverWait(self.driver, 30).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, '.product-iWrap'))):
product_name_tmall = element.find_element_by_css_selector('.productTitle a')
product_price_tmall = element.find_element_by_css_selector('.productPrice em')
tmallSpider.items['product_name_tmall'] = product_name_tmall.get_attribute('title')
tmallSpider.items['product_price_tmall'] = product_price_tmall.get_attribute('title')
yield tmallSpider.items
您试图使用无效的CSS选择器。
::text
伪选择器不是任何实际CSS规范的一部分。有可能,甚至有可能,Scrapy支持不属于CSS标准的选择器。jQuery也曾这样做,非标准选择器支持对Selenium用户造成了不小的困惑,因为Selenium只支持浏览器本机选择器引擎支持的CSS部分
另外,如果您使用的是CSS选择器,但不确定它是否正确,则可以打开浏览器的开发人员工具,并转到其JavaScript控制台。键入document.querySelector(“”)
并按enter键。如果语句返回一个元素,您应该能够在Selenium代码中使用选择器。否则,您将无法成功使用它
在这种特定情况下,我将执行以下操作:
for element in WebDriverWait(self.driver, 30).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, '.product-iWrap'))):
product_name_tmall = element.find_element_by_css_selector('.productTitle a')
product_price_tmall = element.find_element_by_css_selector('.productPrice em')
tmallSpider.items['product_name_tmall'] = product_name_tmall.get_attribute('title')
tmallSpider.items['product_price_tmall'] = product_price_tmall.get_attribute('title')
yield tmallSpider.items
您可以共享页面的URL吗?如果您想获取有关所有产品的信息,请使用selenium和store中的FindElementsit@JustinLambert你想搜索“Ipad”并获取所有产品吗?只需在第一页搜索产品。搜索将是不同的输入。但是iPad是其中之一,你能分享页面的URL吗?如果你想获取所有产品的信息,你需要使用selenium和store中的FindElementsit@JustinLambert你想搜索“Ipad”并获取所有产品吗?只需在第一页搜索产品。搜索将是不同的输入。但iPad是其中一个精彩的解释@JimEvans精彩的解释@JimEvans