我抓取的网页包含以下元素。
<a class="a-declarative" href="javascript:void(0)"
data-action="a-expander-toggle"
data-a-expander-toggle='{"allowLinkDefault":true,
"expand_prompt":"See more", "collapse_prompt":"See less"}'>
<i class="a-icon a-icon-extender-expand"></i>
<span class="a-expander-prompt">See more</span>
</a>
我希望能够点击这个&#34;查看更多&#34;在selenium然后解析响应。我在我的scrapy蜘蛛中有这个。
seemore = response.xpath('//a[contains(@data-action,"a-expander-toggle")]')
seemore.click()
但是当我执行seemore.click()时,它会出错。如果我打印我的xpath选择器,这就是我所看到的。
# Log the "see more" link
self.log(response.xpath('//a[contains(@data-action,"a-expander-toggle")]'))
# Log output
[<Selector xpath='//a[contains(@data-action,"a-expander-toggle")]'
data=u'<a href="javascript:void(0)" data-action'>]
可能没有正确阅读链接。我的xpath是否正确选择这种链接?
答案 0 :(得分:0)
是的,正如Valdir Stumm Junior所说,我相信你需要将click功能称为webdriver元素。可能不是最优雅的例子,但这里有一些(未经测试的)示例代码可以适应您的情况..
from selenium.common.exceptions import NoSuchElementException
...
def parse_page(self, response):
self.driver.get(response.url)
try:
seemore = driver.find_element_by_class_name('a-expander-prompt')
except NoSuchElementException:
# insert how you want to handle this e.g. break, log etc
seemore.click()
有关如何获取硒元素处理的更多详细信息:
http://selenium-python.readthedocs.io/locating-elements.html