您好,我有这个xpath代码,我想要链接和数据。Xpath元素在Selenium中不匹配但在浏览器控制台中匹配
<li class="qTile P-14 Bdbx-1g Bgc-w">
<div class="Lh-16 ">
<h3 id="20151012074222AAY5Tdd" class="qstn-title Fz-15 Fw-b Wow-bw"><a data-rapid_p="1" class="Clr-b" data-ylk="slk:qtitle" href="/question/index?qid=20151012074222AAY5Tdd">Google or Yahoo?</a></h3>
<div class="desc">
Both
</div>
<div class="long-desc Mah-130 Ovy-s D-n">
Both
</div>
<div class="Fz-12 Clr-888">
75 answers
<span class="Fz-14">·</span>
<a data-rapid_p="2" class="Clr-b" data-ylk="slk:cat" href="/dir/index/discover?sid=2115500141">Google</a>
<span class="Fz-14">·</span>
3 days ago
</div>
在这张图中只显示数据字段,xpath用于取问题的链接效果很好。 我尝试使用这个xpath并在浏览器中运行良好,但是当我在Python中使用硒时,我有xpath错误。
post_elems = self.driver.find_elements_by_xpath('//li[contains(@class,"qTile P-14 Bdbx-1g Bgc-w")]')
i = 0
for post in post_elems:
data_of_question = post.find_element_by_xpath('.//div[contains(@class,"Fz-12 Clr-888")]/text()[last()]')
url = post.find_element_by_xpath('.//h3/a[contains(@class,"Clr-b")]')
url_accodare = url.get_attribute('href')
请将HTML作为文本而不是图片发布,因为在某些时候图片将不再可用并且问题将不太有用。 – JeffC