Search code examples
pythonseleniumfirefoxgeckodriverselenium-firefoxdriver

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


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) 

Solution

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