【问题标题】:How to get the content in the mouse floating window?如何获取鼠标浮动窗口中的内容?
【发布时间】:2021-09-08 06:32:45
【问题描述】:

浮动窗口的内容不包含在网页代码中。它仅在鼠标悬停时出现。那么在这种情况下如何获得价值呢?我觉得这很棘手,因为我没有锚点来控制鼠标的移动。

代码如下,

def button_click(driver, button_num):
    driver.execute_script("arguments[0].click();", button_num)


def catogory_obtain_tokyo(driver):
    time_waiting_max = 20
    try:
        page_kansai = WebDriverWait(driver, time_waiting_max).until(
            EC.presence_of_element_located((By.ID, 'snippet-13'))
        )

        buttons = WebDriverWait(page_kansai, time_waiting_max).until(
            EC.presence_of_all_elements_located((By.CLASS_NAME, "mv-button-base.mv-hyperlink-button"))
        )
        return buttons
    except:
        print('catogory_obtain error')
        driver.quit()
        return ''

path = r'chromedriver.exe'
tokyo_url = r'https://www.eex.com/en/market-data/power/futures#%7B%22snippetpicker%22%3A%22EEX%20Japanese%20Power%20Futures%20-%20Tokyo%22%7D'

# --- time line ---
timeline = '//*[@id="null"]/div/div[2]/div'

# ------- price trade reg ----
pane_pr = '//*[@id="null"]/div/div[1]/div[1]/div[2]'

# --------volume trade registration ------
pane_vtr = '//*[@id="null"]/div/div[1]/div[3]/div[2]'

driver = webdriver.Chrome(path)
driver.get(tokyo_url)

btns = catogory_obtain_tokyo(driver)
button_click(driver, btns[0])

time.sleep(3)

# sep-03 btn
date = '//*[@id="symbolheader_jft"]/div/div[1]/div[2]/table/tbody/tr[1]/td[5]'
date_btn = WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.XPATH, date))
)
time.sleep(5)
date_btn.click()

# hit icon
icon_path = '//*[@id="baseloadwidget_jft"]/table/tbody/tr[2]/td[5]'
icon = WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.XPATH, icon_path))
)

time.sleep(5)
icon.click()

time.sleep(5)

# --------- click volume btn ------
vtr_path = '//*[@id="baseloadwidget_jft"]/table/tbody/tr[3]/td/div/div[2]/div[3]/div[2]'
vtr_btn = WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.XPATH, vtr_path))
)
time.sleep(5)
vtr_btn.click()

time.sleep(5)

tl = driver.find_element(By.XPATH, timeline)
webdriver.ActionChains(driver).move_to_element(tl).perform()
time.sleep(5)

pr = driver.find_element(By.XPATH, pane_pr)
webdriver.ActionChains(driver).move_to_element(pr).perform()
time.sleep(5)

vtr = driver.find_element(By.XPATH, pane_vtr)
webdriver.ActionChains(driver).move_to_element(vtr).perform()
time.sleep(5)

time.sleep(5)
driver.quit()

基本上,我尝试了move_to_element 方法,但它只会将鼠标移动到元素的中心。但是,这里这个内联图表被认为是一个元素,如何控制鼠标在一个 Web 元素内的移动?

【问题讨论】:

  • 您是指鼠标悬停执行的操作吗?
  • @fam 是的,像这样。但是我怎样才能得到我的鼠标和数据表之间的相对位置呢?实际上,我希望鼠标在元素内移动。
  • 请提供当前的代码示例和您尝试方法的网站。
  • 这是website。请在名为week 36/21week 类别中查看2021-09-03 的数据,单击行尾的图标,您可以看到折线图。这是我要从中抓取数据的表。
  • @Leo_Liu - 预期输出是什么?

标签: python-3.x selenium


【解决方案1】:

我希望这是您正在寻找的答案。

from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains


driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://www.eex.com/en/market-data/power/futures#%7B%22snippetpicker%22%3A%22EEX%20Japanese%20Power%20Futures%20-%20Tokyo%22%7D")
time.sleep(30) # Manually selected the options.
blocks = driver.find_elements_by_xpath("//div[@class='mv-panes-host']/div[3]/div[2]//*[local-name()='svg']/*[name()='g'][2]//*[name()='rect']")
actions = ActionChains(driver)
for block in blocks:
    actions.move_to_element(block).perform()
    time.sleep(2)
    print(driver.find_element_by_xpath("//div[@id='null']/div/div[1]/div[3]/div[2]/div/div[contains(@class,'date')]").text)
    print(driver.find_element_by_xpath("//div[@id='null']/div/div[1]/div[3]/div[2]/div//div[contains(@class,'name')]").text)
    print(driver.find_element_by_xpath("//div[@id='null']/div/div[1]/div[3]/div[2]/div//div[contains(@class,'value')]").text)


driver.quit()
9/3/2021, 01:53:23 PM
Volume Trade Registration
840.000
9/3/2021, 01:56:26 PM
Volume Trade Registration
840.000

【讨论】:

  • 请问你为什么要这样写 xpath //div[@class='mv-panes-host']/div[3]/div[2]//*[local-name()='svg']/*[name()='g'][2]//*[name()='rect'] ?我使用 xpath //*[@id="null"]/div/div[1]/div[3]/div[2]/svg/g[2]/g/g/*,但它不起作用。
  • @Leo_Liu -因为我们试图找到一个svg标签的元素。如果您在 DOM 中观察,将检测到 //*[@id="null"]/div/div[1]/div[3]/div[2] 的元素,但不会检测到 /svg。要在svg 标签中找到元素,我们需要提供类似//*[name()="svg"][local-name()='svg'] 的xpath
猜你喜欢
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多