使用Selenium在鼠标悬停点从图形中提取数据

2024-05-23 23:47:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我没有API访问权限

我正在使用Selenium(Python)完成这项任务
图显示在这里,https://www.highcharts.com/demo/stock/flags-general/dark-unica

更具体地说,我需要鼠标悬停到特定点时显示的数据

以下是我尝试过的方法:

  1. 提取“路径”标记的“d”部分时,我会得到与网站上显示的不同的值,因此无法搜索特定点
  2. 尝试获取页面源代码时,执行此操作后,我将获取所有空值
  3. 用于检测点的“scrollintoview”选项,返回空列表

Tags: 数据方法httpscomapi权限demowww
2条回答

此Xpath用于查找点A、B和C处的值

//div//*[name()='svg']//*[name()='g']//*[name()='g' and @class="highcharts-markers highcharts-series-1 highcharts-flags-series highcharts-color-1 highcharts-tracker"]//*[@data-z-index='1']

我不完全确定您正在尝试执行哪些操作,但假设您已经将鼠标悬停在某个点上,要获取文本,请使用以下命令:

要仅获取USD to EUR:文本,请使用:

driver.find_elements_by_css_selector(".highcharts-label.highcharts-tooltip-box.highcharts-color-0>text[data-z-index]>tspan:nth-of-type(1)")

要获取货币值,请使用:

driver.find_elements_by_css_selector(".highcharts-tooltip-box.highcharts-color-0>text[data-z-index]>tspan:nth-of-type(2)")

注意,在第二个定位器中,我删除了一个类,定位器仍然是唯一的

因此,方法是使用悬停动作。为此,您需要使用ActionChains: 如何使用的小示例:

from selenium.webdriver import ActionChains

locator = driver.find_element_by_xpath("xpath where you need to move a mouse")
actions = ActionChains(driver)
actions.move_to_element(locator)
actions.click().perform()

单击所需的点后,按如下方式提取文本:

currency_value = driver.find_elements_by_css_selector(".highcharts-tooltip-box.highcharts-color-0>text[data-z-index]>tspan:nth-of-type(2)").text

相关问题 更多 >