我試圖拿開,高,低,本網站上密切互動圖表值找元素。的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-content
,Message: 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)
該網站上的元素有正確的ID,那麼爲什麼你在地球上不使用它們,而是把這個瘋狂的xpath與索引?你怎麼知道它甚至是正確的? –
爲什麼你的代碼中有絕對的xpath?即使你的xpath是正確的,當餘弦下的dom改變時它也會失敗。另外,請在網頁中向我們指出高,低等。 – testerjoe2
@KirilS。我沒有找到任何ID,請告訴我在哪裏尋找他們,以便我可以相應地更改腳本以使其工作。 –