[英]Get the value of in html span selenium python
binary.com 上的某個跨度圖標具有以下 html 代碼:
<span id="spot" style="" data-value="3862.76" class="price_moved_down">3,862.76</span>
其中數據值每 2 秒更改一次。 我想在我的網絡自動化腳本中使用該數據值,但我不知道從哪里開始,請幫忙。 看圖就明白了。
要打印文本3,862.76,您可以使用以下任一定位器策略:
使用css_selector
和get_attribute()
:
print(driver.find_element_by_css_selector("span.price_moved_down#spot[data-value]").get_attribute("innerHTML"))
使用xpath
和text屬性:
print(driver.find_element_by_xpath("//span[@class='price_moved_down' and @id='spot'][@data-value]").text)
理想情況下,要打印文本3,862.76,您必須為visibility_of_element_located()
引入WebDriverWait ,您可以使用以下任一定位器策略:
使用CSS_SELECTOR
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.price_moved_down#spot[data-value]"))).get_attribute("innerHTML"))
使用XPATH
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='price_moved_down' and @id='spot'][@data-value]"))).text)
注意:您必須添加以下導入:
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.
數字是元素的文本值。 所以一旦你找到了使用硒的元素
my_span = driver.find_element_by.....
您可以只調用元素上的 text 屬性
print(my_span.text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.