简体   繁体   English

列出 HTML 范围内的动态 aria-label 中的值

[英]List values from dynamic aria-label inside HTML span

I need 5-min prices for some small stocks (yfinance doesn't have them in 5-min increments).我需要一些小股票的 5 分钟价格(yfinance 没有以 5 分钟为增量的价格)。 Robinhood displays 5-min prices when you mouse hover over the graph.当您将 hover 鼠标悬停在图表上时,Robinhood 会显示 5 分钟的价格。 These values are stored in as follows: HTML span element这些值存储如下: HTML 跨度元素

I hoped this might return a list of values, but no luck:我希望这可能会返回一个值列表,但没有运气:

first_rev = browser.find_element_by_xpath("/html/body/div[1]/main/div[2]/div/div/div/div/div/main/div/div[1]/section[1]/header/div[1]/h2/span/span")
first_rev.click()
aria_label = first_rev.get_attribute("aria-label")
print(aria_label)

Thanks in advance.提前致谢。

According to the DOM in your snapshot, aria-label is for a single-element which encloses 5 elements inside it with id sdp-market-price-tooltip .根据您快照中的 DOM, aria-label用于单个元素,其中包含 5 个元素,其中 id sdp-market-price-tooltip You have use something like this:你已经使用了这样的东西:

prices = driver.find_elements_by_id("sdp-market-price-tooltip")
print(len(prices) # you should get 5 as output here
each_price = [price.text for price in prices]
print(each_price)

You may use some extended xpath also, in case the id locator does not work.您也可以使用一些扩展的 xpath,以防 id 定位器不起作用。 prices = driver.find_element_by_xpath("//div[@id='sdp-market-price']//span[@id='sdp-market-price-tooltip'] , etc. prices = driver.find_element_by_xpath("//div[@id='sdp-market-price']//span[@id='sdp-market-price-tooltip']

value=driver.find_element(By.XPATH,"//h2[@data-testid='PortfolioValue']/span[1]/span[1]").get_attribute("aria-label")
print(value)

You can grab the span's attribute value like so.您可以像这样获取跨度的属性值。

Furthermore to allow for value to pop up on the screen we use waits.此外,为了让值在屏幕上弹出,我们使用等待。

wait=WebDriverWait(driver,60)
value=wait.until(EC.visibility_of_element_located((By.XPATH,"//h2[@data-testid='PortfolioValue']/span[1]/span[1]")))

Imports:进口:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM