我正在嘗試從以下代碼示例中獲取一些值,但無法獲得成功的答案。
使用在此鏈接中輸入的隨機商標的美國專利商標局網站。我想獲取值為 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>
請注意,這將是value代碼示例中第四次呼叫類名并嵌套在其他 div 類中。
這是我到目前為止所嘗試的:
values = browser.find_elements(By.CLASS_NAME, 'value')
print(values[3])
但 values 回傳一個空串列 []
請告知我做錯了什么,提前謝謝。
uj5u.com熱心網友回復:
要列印文本May 12, 2015,您可以使用以下任一定位器策略:
使用xpath和text屬性:
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
您可以在如何使用 Selenium - Python 檢索 WebElement 的文本中找到相關討論
參考
鏈接到有用的檔案:
get_attribute()方法Gets the given attribute or property of the element.text屬性回傳The text of the element.- 使用 Selenium 的 text 和 innerHTML 之間的區別
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/411722.html
標籤:
