[英]Python/Selenium: How to grab value from the HTML?
我正在嘗試從以下代碼示例中獲取一些值,但無法獲得成功的答案。
使用在此鏈接中輸入的隨機商標的美國專利商標局網站。 我想獲取值為 2015 年 5 月 12 日的注冊日期
<div class="double table">
<div class="row">
<div class="key">US Serial Number:</div>
<div class="value">85931937</div>
<div class="key">Application Filing Date:</div>
<div class="value">May 14, 2013</div>
</div>
<div class="row">
<div class="key">US Registration Number:</div>
<div class="value">4735834</div>
<div class="key">Registration Date:</div>
<div class="value">May 12, 2015</div>
</div>
請注意,這將是代碼示例中第四次調用 class 名稱value
並嵌套在其他 div 類中。
這是我到目前為止所嘗試的:
values = browser.find_elements(By.CLASS_NAME, 'value')
print(values[3])
但 values 返回一個空列表[]
請告知我做錯了什么,提前謝謝。
要打印文本May 12, 2015
您可以使用以下任一定位器策略:
使用xpath和文本屬性:
print(driver.find_element(By.XPATH, "//div[@class='key' and text()='Registration Date:']//following-sibling::div[1]").text)
理想情況下,您需要為visibility_of_element_located()引入WebDriverWait ,並且您可以使用以下任一Locator Strategies :
使用XPATH和get_attribute("innerHTML")
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='key' and text()='Registration Date:']//following-sibling::div[1]"))).get_attribute("innerHTML"))
注意:您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
鏈接到有用的文檔:
get_attribute()
方法Gets the given attribute or property of the element.
text
屬性返回The text of the element.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.