您好,我有這個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