How should I properly use Selenium

非 Y 不嫁゛ 提交于 2019-12-04 19:24:46

Actually your all locator looks like invalid, try using find_element_by_css_selector as below :-

elem = browser.find_element_by_css_selector("span[data-reactid *= 'TOTAL_STOCKHOLDER_EQUITY']")

Note: find_element_by_partial_text is use to locate only a with paritially match of text content not their attribute text and find_element_by_id is use to locate any element with their id attribute which will match exactly with passing value.

Edited :- There are more elements found with the provided locator, so you should try to find exact row of Total Stockholder Equity means tr element then find all their td elements as below :-

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

browser = webdriver.Chrome()
browser.get('http://finance.yahoo.com/quote/AAPL/financials?p=AAPL')
browser.maximize_window()

wait = WebDriverWait(browser, 5) 

    try:
        #first try to find balance sheet link and click on it
        balanceSheet = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text() = 'Balance Sheet']")))
        balanceSheet.click() 

        #Now find the row element of Total Stockholder Equity
        totalStockRow = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "tr[data-reactid *= 'TOTAL_STOCKHOLDER_EQUITY']")))

        #Now find all the columns included with Total Stockholder Equity
        totalColumns = totalStockRow.find_elements_by_tag_name("td")

        #Now if you want to print single value just pass the index into totalColumns other wise print all values in the loop

        #Now print all values in the loop
        for elem in totalColumns:
             print elem.text
             #it will print value as 
             #Total Stockholder Equity
             #119,355,000
             #111,547,000
             #123,549,000
    except:
        print('Was not able to find the element with that name.')

Hope it helps...:)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!