简体   繁体   中英

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). Robinhood displays 5-min prices when you mouse hover over the graph. These values are stored in as follows: HTML span element

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 . 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. prices = driver.find_element_by_xpath("//div[@id='sdp-market-price']//span[@id='sdp-market-price-tooltip'] , etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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