2017-06-06 49 views
0

我試圖拿開,高,低,本網站上密切互動圖表值找元素。的Python:<a href="https://www.investing.com/currencies/eur-usd-chart" rel="nofollow noreferrer">Currency Chart from website</a>:硒通過XPath返回空列表

這是元素的XPath://*[@id="chart-area"]/div/div[3]/table/tbody/tr[1]/td[2]/div/div[3]/div[2]/div/span[1]

我已經加入對HTML調試器屬性name,試圖find_elements_by_name,但它也返回一個空列表。 find_by_class_name方法返回無法搜索化合物值的錯誤。每當將鼠標懸停在圖表上並將其打印在控制檯中時,我需要獲取「打開」,「高」,「低」和「關閉」值。

我試圖在同一時間,我移動鼠標在圖表區域來獲取值。我不知道這是否是因爲該字段是動態的,因爲它隨着我在圖表上滾動而發生變化。

編輯:值位於圖的左上角,當你在圖表區域移動鼠標,光標將捕捉到的點和數據得到更新。即使在數據,Raw Data Website的原始版本,我可以用class names="line-content"css_selector = 'html body'xpath='/html/body/table/tbody/tr/td[2]'時拿不到元素的含量。

輸出是這樣的:Message: Unable to locate element: .line-contentMessage: Unable to locate element: /html/body/table/tbody/tr/td[2]

我的代碼:

driver = webdriver.Firefox() 
driver.get("https://www.investing.com/currencies/eur-usd-chart") 
open_value_1 = driver.find_elements(By.XPATH, '//*[@id="chart-area"]/div/div[3]/table/tbody/tr[1]/td[2]/div/div[3]/div[2]/div/span[1]') 

# If mouse moved and the value changed - print it 
while True: 
    open_value_2 = driver.find_elements(By.XPATH, '//*[@id="chart-area"]/div/div[3]/table/tbody/tr[1]/td[2]/div/div[3]/div[2]/div/span[1]') 
    if open_value_2 is not open_value_1: 
     print(open_value_2) 
    time.sleep(1) 
+1

該網站上的元素有正確的ID,那麼爲什麼你在地球上不使用它們,而是把這個瘋狂的xpath與索引?你怎麼知道它甚至是正確的? –

+0

爲什麼你的代碼中有絕對的xpath?即使你的xpath是正確的,當餘弦下的dom改變時它也會失敗。另外,請在網頁中向我們指出高,低等。 – testerjoe2

+0

@KirilS。我沒有找到任何ID,請告訴我在哪裏尋找他們,以便我可以相應地更改腳本以使其工作。 –

回答

-1

也許有一些問題與您的XPath

(//*[@id="chart-area"]/div/div[3]/table/tbody/tr[1]/td[2]/div/div[3]/div[2]/div/span[1]) 

你可以嘗試波紋管的XPath

//*[@id="chart-area"]/div/div[3]/table/tbody/tr[1]/td[2]/div/div[3]/div[1]/div/span[1] 
+0

或這一個// * [@ id =「chart-area」]/div/div [3]/table/tbody/tr [1]/td [2]/div/div [3]/div [1]/div/span [1] // span [@ class =「pane-legend-item-value pane-legend-line main」] –

+0

此xpath還返回錯誤'is invalid:SyntaxError:The expression is不是法律上的表達方式。「正如@KirilS所解釋的,這可能是因爲網站上有多個框架。 –

+0

對我來說Xpath正在運行@VascoFerreira // * [@ id =」chart-area「]/div/DIV [3] /表/ tbody的/ TR [1]/TD [2]/DIV/DIV [3]/DIV [1]/DIV /跨度[1] /跨度[@類=「窗格-圖例-本期特價貨品值標題窗格-圖例線「] –

相關問題