3

I need to change the position of the Firefox window by creating the driver with:

driver = webdriver.Firefox()

I know it's possible to change the window position after the driver was created:

driver.set_window_position()

I can't find out how to do it using Firefox profile or options:

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

or

options = Options()
options.some_optins = my_options

and finally:

driver = Webdriver.Firefox(firefox_profile=profile, options=options) 
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Mykola Zotko
  • 15,583
  • 3
  • 71
  • 73

1 Answers1

1

You saw it right.

set_window_position()

set_window_position() sets the x,y position of the current window.

  • Implementation:

      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)
    
  • Definition:

      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
                   })
    

So to summarize, window_position is coupled to the window handle pertaining to the browser and can be handled by webdriver instance only.

This functionality can't be handled either through:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • According to this [answer](https://stackoverflow.com/a/37151037/8973620) you can do it in Chrome. It should be possible in Firefox as well. – Mykola Zotko Feb 08 '19 at 19:30
  • 3
    And to boot `driver.set_window_position(0,0)` actually positions the window about 10 pixels in from the left , so it's not even working correctly. (Windows10) A value of -5 for the x coord worked for me, it seems someone added the system metrics SM_CXBORDER furniture value twice? –  Oct 19 '20 at 18:18