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

Python 如何使用FirefoxProfile或FirefoxOptions通过Selenium设置Firefox浏览器的窗口位置,python,selenium,firefox,geckodriver,selenium-firefoxdriver,Python,Selenium,Firefox,Geckodriver,Selenium Firefoxdriver,我需要通过创建以下驱动程序来更改Firefox窗口的位置: driver = webdriver.Firefox() 我知道在创建驱动程序后可以更改车窗位置: driver.set_window_position() 我无法找到如何使用Firefox配置文件或选项进行此操作: profile = webdriver.FirefoxProfile() profile.set_preference("some_preference", my_preference) 或 最后: driver =

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

driver = webdriver.Firefox()
我知道在创建驱动程序后可以更改车窗位置:

driver.set_window_position()
我无法找到如何使用Firefox配置文件或选项进行此操作:

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

最后:

driver = Webdriver.Firefox(firefox_profile=profile, options=options) 
你看得对

设置窗口位置() 设置当前窗口的
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\u position
与浏览器的窗口句柄耦合,只能由webdriver实例处理

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

  • firefox\u配置文件
    ->:设置配置文件中所需的首选项
  • firefox.options
    ->:设置首选项

根据这一点,您可以在Chrome中完成。这在Firefox中也应该是可能的。要启动
驱动程序,设置窗口位置(0,0)
实际上将窗口定位在距离左侧约10像素的位置,因此它甚至不能正常工作。(Windows10)x坐标的值为-5对我有效,似乎有人将系统度量SM_CXBORDER值添加了两次?
  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
               })