带有selenium webdriver的鼠标滚轮,在没有滚动条的元素上?

时间:2017-11-13 22:47:40

标签: python selenium selenium-webdriver

我试图驾驶类似于Google地图的网络地图的一部分,其中放大/缩小是通过在鼠标悬停时滚动来完成的。理想情况下,我希望能够做到这样的事情:

count = 0

while (count+1)**3 < 12000:
  count += 1

print("The largest integer <12000 is", count)

我在文档中看到的最接近的方法是someElement.scroll(-50) click,但这些方法都没有滚动。我还尝试通过Javascript将卷轴发送到页面,例如send_keys但这似乎无能为力。

我该怎么做?

4 个答案:

答案 0 :(得分:11)

要重现/测试鼠标滚轮,您必须使用脚本注入将mouseovermousemovewheel事件发送到顶部元素。

以下是谷歌地图的一个实例:

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地图发送缩放+/-缩放:

# 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("-")

答案 1 :(得分:0)

在谷歌地图上,有一个放大/缩小按钮。您可以使用它而不是鼠标滚动。

//点击放大

driver.find_element_by_id('widget-zoom-in').click()

//点击缩小

driver.find_element_by_id('widget-zoom-out').click()

答案 2 :(得分:0)

您可以使用PyRobot类尝试它,首先关注您要执行放大/缩小的点,然后使用PyRobot类通过键盘键执行放大/缩小操作。

答案 3 :(得分:0)

通过将JS代码段外包到一个单独的文件中,并将其包装为带有可读参数名的旧式JS模块格式,可以稍微改善佛罗伦萨的答案。

文件名为simulate_wheel.js和:

/* global arguments */
(function (element, deltaY, offsetX, offsetY) {
    var box = element.getBoundingClientRect();
    var clientX = box.left + (offsetX || box.width / 2);
    var clientY = box.top + (offsetY || 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";
}).apply(null, arguments);

随后可以阅读和使用以下内容

# Load it using the module loader, the module in this example is called "helper_js"
# Alternatively, simple read functions could be used
import pkgutil
wheel_js = pkgutil.get_data("helper_js", "simulate_wheel.js").decode("utf8")

def simulate_wheel(element, deltaY=120, offsetX=0, offsetY=0):
    error = element._parent.execute_script(wheel_js, element, deltaY, offsetX, offsetY)
    if error:
        raise WebDriverException(error)

这是similar to how it's down inside the Selenium bindings for Python