【问题标题】:Why does selenium's move_by_offset function sometime waits before executing (Python/Chrome)为什么 selenium 的 move_by_offset 函数有时会在执行前等待(Python/Chrome)
【发布时间】:2018-11-03 12:41:53
【问题描述】:

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

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

由于某些原因,上述操作至少需要 5 秒,例如5.03123 秒,执行。当有延迟时,它总是略高于 5,但从不低于 5,这让我相信某个地方有 time.sleep(5)。我检查了 selenium actionchains 文件并注释掉了:

self.w3c_actions.key_action.pause()

万一这是罪魁祸首,但没有显着变化。

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

我非常不知道为什么会发生这种情况,并且尝试了很多不同的东西/测试,但基本上无济于事。非常感谢任何和所有帮助。

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

【问题讨论】:

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


    【解决方案1】:

    您可以覆盖该方法:

    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/

    【讨论】:

      【解决方案2】:

      注释掉这行你做错了:

      self.w3c_actions.key_action.pause()
      

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

      窗口最小化

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

      你会在Way to open Selenium browser not ovelapping my current browser找到相关讨论

      【讨论】:

      • 为什么问题超出范围?
      猜你喜欢
      • 1970-01-01
      • 2012-03-06
      • 2014-12-03
      • 2011-08-13
      • 2017-08-11
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 2020-01-23
      相关资源
      最近更新 更多