我应该如何正确使用硒 [英] How should I properly use Selenium
问题描述
我正在尝试从Yahoo Finance( http://finance.yahoo.com/quote/AAPL/financials?p=AAPL ),资产负债表,总股东权益.如果我检查元素,我会得到:
I'm trying to get one number from Yahoo Finance (http://finance.yahoo.com/quote/AAPL/financials?p=AAPL), Balance Sheet, Total Stockholder Equity. If I inspect the element I get this:
<span data-reactid=".1doxyl2xoso.1.$0.0.0.3.1.$main-0-Quote-Proxy.$main-0-Quote.0.2.0.2:1:$BALANCE_SHEET.0.0.$TOTAL_STOCKHOLDER_EQUITY.1:$0.0.0">119,355,000</span>
我想得到的是报废号码:119,355,000.
I would like to get, scrap the number: 119,355,000.
如果我理解正确,则网页使用Java Script编码,我需要使用Selenium才能获得所需的编号.无论我做什么,我的尝试(我是初学者)都没有用,贝娄是许多尝试中的三种.我试图使用'data-reactid'和其他一些内容,但我的构想已不多了:-)
If I understand correctly, web page is coded in Java Script and I need to use Selenium to get to the desired number. My attempt (I'm complete beginner) is not working no matter what I do, Bellow are three of many attempts. I tried to use 'data-reactid' and few other tings and I'm running out of ideas :-)
elem = Browser.find_element_by_partial_link_text('TOTAL_STOCKHOLDER_EQUITY')
elem = browser.find_element_by_id('TOTAL_STOCKHOLDER_EQUITY')
elem = browser.find_elem_by_id('TOTAL_STOCKHOLDER_EQUITY')
推荐答案
实际上,您的所有定位器看起来都是无效的,请尝试使用find_element_by_css_selector
,如下所示:-
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']")
注意:find_element_by_partial_text
仅用于查找文本内容部分匹配的a
,而不是其属性文本,而find_element_by_id
用于查找具有id
属性的元素,该元素将与传递值完全匹配.
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.
已编辑:-通过提供的定位器找到了更多元素,因此您应该尝试查找Total Stockholder Equity
表示tr
元素的确切行,然后查找其所有td
元素,如下:-
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.')
希望有帮助...:)
这篇关于我应该如何正确使用硒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!