簡體   English   中英

如何使用Python使用Selenium Webdriver獲取此html中的一些文本值?

[英]How to get the some text value in this html with Selenium Webdriver using Python?

我如何從下面的html代碼中使用selemiun和python獲取文本值“ This Text 1”和“ This Text 2”:

<p>
   <b>Studio:</b>
   <a href="http://www.somelink.com/some_page/">This Text 1</a>
   " | "
   <b>Director:</b> This Text 2
</p>

我已經嘗試過幾種類似的方法:

driver.find_element_by_xpath("//*[contains(text(), 'Director:')]")

但沒有得到結果。

要獲取文本“ This Text 1和“ This Text 2請使用以下選項。

獲取xpath This Text 1使用以下xpath

print(driver.find_element_by_xpath("//p/b[contains(.,'Studio')]/following::a").text)

使用JavaScript Executor獲取此文This Text 2

element=driver.execute_script('return arguments[0].lastChild.textContent;', driver.find_element_by_xpath("//p[contains(.,'Director')]"))
print(element.strip())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM