MouseEvent不是构造函数| PhantomJS/Selenium WebDriver(Python)

2024-06-26 00:06:32 发布

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

我将以下代码直接从DevTool的控制台复制并粘贴到driver.execute_script块中。从浏览器执行时,观察到的行为与我预期的一样。但是,当通过execute_script执行时,我从Selenium得到一个MouseEvent is not a constructor错误

这是密码

self.driver.execute_script('''
        var mousedownEvent = new MouseEvent('mousedown');
        mousedownEvent.initMouseEvent('mousedown', true, true);
        arguments[0].dispatchEvent(mousedownEvent);
''', dropdown_container.find_element_by_css_selector('span.select2-chosen'))

Tags: 代码trueexecuteis粘贴driverseleniumscript
1条回答
网友
1楼 · 发布于 2024-06-26 00:06:32

问题是PhantomJS不支持我用来创建鼠标事件的API。在

相反,这是我使用的代码:

self.driver.execute_script('''
    var me = document.createEvent("MouseEvent");
    me.initEvent(
        "mousedown",
        true,
        true,
    );
    arguments[0].dispatchEvent(me);
''', my_element)

我在PhantomJS的网站上找不到任何关于事件支持API的文档,也没有在源代码之外的GhostDriver中看到太多。如果有人知道信息的位置,我会留下这个问题,并且可以发布更详细的解释

相关问题 更多 >