如何使用 Selenium 和 Python 通过 href 提取文本

How to extract the text through a href using Selenium and Python

我查看了每个帖子,但没有得到我想要的解决方案。

browser.find_element_by_xpath("//*/section/main/div/div[3]/article/div[1]/div/div[1]/div[1]/a")
pic = browser.find_elements_by_xpath("//*/section/main/div/div[3]/article/div[1]/div/div[1]/div[1]/a").get(title)
print(title)

这就是我刚才的代码,但我正需要这个:

<a href="/p/BrDudIUBuNr/"><div class="eLAPa"><div class="KL4Bh"><img class="FFVAD" decoding="auto" sizes="281.671875px" srcset="https://scontent-frx5-1.cdninstagram.com/vp/38a5fdde34937b2b3e4e600da56c46b5/5C0D05B3/t51.2885-15/e15/s150x150/46276509_388170972011907_7609813800358803282_n.jpg 150w,https://scontent-frx5-1.cdninstagram.com/vp/74b222ca252a488bb5eb110347283c3b/5C0CB7F9/t51.2885-15/e15/s240x240/46276509_388170972011907_7609813800358803282_n.jpg 240w,https://scontent-frx5-1.cdninstagram.com/vp/af2eff79d9c38b0f97a763041e3a99e5/5C0D10C3/t51.2885-15/e15/s320x320/46276509_388170972011907_7609813800358803282_n.jpg 320w,https://scontent-frx5-1.cdninstagram.com/vp/bad123f6264636de35489b7d8dc2cbed/5C0CC199/t51.2885-15/e15/s480x480/46276509_388170972011907_7609813800358803282_n.jpg 480w,https://scontent-frx5-1.cdninstagram.com/vp/93a4e36addf453f754e4caba706fe93a/5C0CC82C/t51.2885-15/e15/s640x640/46276509_388170972011907_7609813800358803282_n.jpg 640w" src="https://scontent-frx5-1.cdninstagram.com/vp/93a4e36addf453f754e4caba706fe93a/5C0CC82C/t51.2885-15/e15/s640x640/46276509_388170972011907_7609813800358803282_n.jpg" style=""></div><div class="_9AhH0"></div></div><div class="u7YqG"><div class="Byj2F"><span class="glyphsSpriteVideo_large u-__7" aria-label="Video"></span></div></div></a>

a href 然后是我需要的 link

要提取 文本 视频,您可以使用以下任一解决方案:

  • css_selector:

    driver.find_element_by_css_selector("a[href='/p/BrDudIUBuNr/'] span.glyphsSpriteVideo_large").get_attribute("aria-label")
    
  • xpath:

    driver.find_element_by_xpath("//a[@href='/p/BrDudIUBuNr/']//span[contains(@class,'glyphsSpriteVideo_large')]").get_attribute("aria-label")