在没有滚动条的元素上

在没有滚动条的元素上

本文介绍了带有硒webdriver的鼠标滚轮,在没有滚动条的元素上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试驱动类似于Google Maps的网络地图的一部分,在该地图上,放大/缩小是通过在鼠标悬停时滚动来完成的.理想情况下,我希望能够执行以下操作:

I'm trying to drive part of a web map akin to Google Maps, where zoom in/out is done by scrolling while moused over. Ideally, I'd like to be able to do something like this:

someElement.scroll(-50)

我在文档中看到的最接近的方法是clicksend_keys,但是它们都不滚动.我也尝试过通过Javascript将滚动条发送到页面,例如driver.execute_script("scroll(0,-50)")这似乎什么也没做.

The closest methods I saw in the documentation were click and send_keys, but neither of those do scrolling. I've also tried sending scrolls to the page via Javascript, e.g. driver.execute_script("scroll(0,-50)") This doesn't seem to do anything though.

我该怎么做?

推荐答案

要重现/测试鼠标滚轮,必须将mouseovermousemovewheel事件发送到顶部元素,并带有脚本注入.

To reproduce/test a mouse wheel, you'll have to emit the mouseover, mousemove and wheel events to the top element with a script injection.

这是Google Map的可行示例:

Here's a working example with Google Map:

from selenium import webdriver
from selenium.common.exceptions import WebDriverException

def wheel_element(element, deltaY = 120, offsetX = 0, offsetY = 0):
  error = element._parent.execute_script("""
    var element = arguments[0];
    var deltaY = arguments[1];
    var box = element.getBoundingClientRect();
    var clientX = box.left + (arguments[2] || box.width / 2);
    var clientY = box.top + (arguments[3] || box.height / 2);
    var target = element.ownerDocument.elementFromPoint(clientX, clientY);

    for (var e = target; e; e = e.parentElement) {
      if (e === element) {
        target.dispatchEvent(new MouseEvent('mouseover', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));
        target.dispatchEvent(new MouseEvent('mousemove', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));
        target.dispatchEvent(new WheelEvent('wheel',     {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY, deltaY: deltaY}));
        return;
      }
    }
    return "Element is not interactable";
    """, element, deltaY, offsetX, offsetY)
  if error:
    raise WebDriverException(error)

options = webdriver.ChromeOptions()
options.add_argument("--disable-infobars --disable-extensions --window-size=1366,768")
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://www.google.co.uk/maps")

# get element
elm = driver.find_element_by_css_selector("#scene > div.widget-scene > canvas")

# zoom in with mouse wheel
wheel_element(elm, -120)

# zoom out with mouse wheel
wheel_element(elm, 120)

作为替代方案,您可以发送Google地图+/-的缩放快捷方式:

As an alternative you could send the zoom shortucts which are +/- with Google map:

# get element
elm = driver.find_element_by_css_selector("#scene > div.widget-scene > canvas")

# zoom in with shortcut
elm.send_keys("+")

# zoom out with shortcut
elm.send_keys("-")

这篇关于带有硒webdriver的鼠标滚轮,在没有滚动条的元素上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 06:37