为什么Selenium的move_by_offset函数有时会在执行之前等待(Python / Chrome)

时间:2018-11-03 12:41:54

标签: python selenium google-chrome selenium-webdriver selenium-chromedriver

我正在使用带有Python的Chrome浏览器中的Selenium来自动化一些测试,而其中的一部分是移动鼠标,因为我创建了大量测试,因此我在线程上并行运行它们。唯一真正给我带来麻烦的代码如下:

action =  selenium.webdriver.common.action_chains.ActionChains(driver)
action.move_by_offset(x,y)
action.perform()

出于某种原因,以上操作至少需要5秒钟,例如5.03123秒,执行。当有延迟时,它总是总是仅略大于5,但绝不小于5,这使我相信某个地方存在time.sleep(5)。我检查了硒动作链文件并注释掉了:

self.w3c_actions.key_action.pause()

如果这是罪魁祸首,但没有重大变化。

重要的一点是,当我的窗口最小化并且有多个线程正在运行时,这似乎是一个更大的问题/经常发生。

对于这种情况的发生,我非常茫然,并且尝试了很多不同的方法/测试,但是基本上没有用。任何帮助都将不胜感激。

如果您需要任何其他信息,或者我应该运行其他特定的测试,请告诉我,我会的。

2 个答案:

答案 0 :(得分:0)

您在注释行时做错了:

self.w3c_actions.key_action.pause()

move_by_offset()方法中,对 key_action.pause() 的调用是出于超出范围的目的而实现的。您绝不会更改源客户端代码

窗口最小化

在执行 Selenium Tests 时,您需要保持窗口最大化

您将在Way to open Selenium browser not ovelapping my current browser

中找到相关的讨论

答案 1 :(得分:0)

您可以覆盖该方法:

class ActionChainsChild(ActionChains):
   def move_by_offset(self, xoffset, yoffset):
        """
        Moving the mouse to an offset from current mouse position.

        :Args:
         - xoffset: X offset to move to, as a positive or negative integer.
         - yoffset: Y offset to move to, as a positive or negative integer.
        """
        if self._driver.w3c:
            self.w3c_actions.pointer_action.move_by(xoffset, yoffset)
            #self.w3c_actions.key_action.pause()
        else:
            self._actions.append(lambda: self._driver.execute(
                Command.MOVE_TO, {
                    'xoffset': int(xoffset),
                    'yoffset': int(yoffset)}))
        return self

实用链接:

https://seleniumhq.github.io/selenium/docs/api/py/_modules/selenium/webdriver/common/action_chains.html

https://www.thedigitalcatonline.com/blog/2014/05/19/method-overriding-in-python/