【问题标题】:How to set window position of Firefox browser through Selenium using FirefoxProfile or FirefoxOptions如何使用 FirefoxProfile 或 FirefoxOptions 通过 Selenium 设置 Firefox 浏览器的窗口位置
【发布时间】:2019-07-01 17:14:18
【问题描述】:

我需要通过创建驱动来改变 Firefox 窗口的位置:

driver = webdriver.Firefox()

我知道在创建驱动程序后可以更改窗口位置:

driver.set_window_position()

我不知道如何使用 Firefox 配置文件或选项:

profile = webdriver.FirefoxProfile()
profile.set_preference("some_preference", my_preference)

options = Options()
options.some_optins = my_options

最后:

driver = Webdriver.Firefox(firefox_profile=profile, options=options) 

【问题讨论】:

    标签: python selenium firefox geckodriver selenium-firefoxdriver


    【解决方案1】:

    你没看错。

    set_window_position()

    set_window_position() 设置当前窗口的x,y 位置。

    • 实施:

        set_window_position(x, y, windowHandle='current')
        Sets the x,y position of the current window. (window.moveTo)
      
        Arguments :
            x: the x-coordinate in pixels to set the window position
            y: the y-coordinate in pixels to set the window position
        Usage :
            driver.set_window_position(0,0)
      
    • 定义:

        def set_window_position(self, x, y, windowHandle='current'):
            if self.w3c:
                if windowHandle != 'current':
                warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
                return self.set_window_rect(x=int(x), y=int(y))
            else:
                self.execute(Command.SET_WINDOW_POSITION,
                     {
                         'x': int(x),
                         'y': int(y),
                         'windowHandle': windowHandle
                     })
      

    总而言之,window_position 与浏览器的窗口句柄耦合,只能由webdriver实例处理。

    此功能也无法通过以下方式处理:

    【讨论】:

    • 根据answer,您可以在 Chrome 中进行操作。在 Firefox 中应该也可以。
    • 并启动driver.set_window_position(0,0) 实际上将窗口定位在距左侧约 10 个像素的位置,因此它甚至无法正常工作。 (Windows10) x 坐标值为 -5 对我有用,似乎有人将系统指标 SM_CXBORDER 家具值添加了两次?
    猜你喜欢
    • 2018-08-09
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 2019-09-29
    相关资源
    最近更新 更多