如何使用 FirefoxProfile 或 FirefoxOptions 通过 Selenium 设置 Firefox 浏览器的 window 位置

How to set window position of Firefox browser through Selenium using FirefoxProfile or FirefoxOptions

我需要通过以下方式创建驱动程序来更改 Firefox window 的位置:

driver = webdriver.Firefox()

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

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) 

你没看错。

set_window_position()

set_window_position()设置当前window.

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 耦合到属于浏览器的 window 句柄,可以由 webdriver 仅限实例。

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