[英]Python Selenium - can't get elements by xpath
<table class="wikitable" style="float: right;">
<caption>Current UTC
</caption>
<tbody><tr>
<td>**January 26, 2020 01:13:27 (UTC) –** <span class="plainlinks" id="purgelink"><span class="nowrap"><a class="external text" href="https://en.wikipedia.org/w/index.php?title=Coordinated_Universal_Time&action=purge">Refresh</a></span></span>
</td></tr></tbody></table>
我只需要得到日期。 我試過:
utc = driver.find_element_by_xpath('//*[@id="mw-content-text"]/div/table[1]/tbody/tr/td/text()')
錯誤:
無效選擇器:xpath 表達式的結果是:[object Text]。 它應該是一個元素。
請幫忙。
這是您要獲取的 xpath 的固定版本。
utc = driver.find_element_by_xpath('//*[@id="mw-content-text"]/div/table[1]/tbody/tr/td')
print(utc.text)
但上面將包括“刷新”鏈接的文本。 要僅獲取您可以嘗試的日期:
utc = driver.find_element_by_xpath('//*[@id="mw-content-text"]/div/table[1]/tbody/tr/td')
utc_trim = utc.get_attribute('innerHTML').split('<')[0]
print(utc_trim)
為了幫助解決頁面加載時間問題,您可能需要執行以下操作:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
utc = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="mw-content-text"]/div/table/tbody/tr/td')))
utc_trim = utc.get_attribute('innerHTML').split('<')[0]
print(utc_trim)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.