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

一笑奈何 提交于 2019-12-11 12:58:43

问题


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) 

回答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)
    
    Args :  
        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)
    
  • Defination:

    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 :

  • firefox_profile -> set_preference(key, value): Sets the preference that we want in the profile.
  • firefox.options -> set_preference(name, value): Sets a preference.


来源:https://stackoverflow.com/questions/54575853/how-to-set-window-position-of-firefox-browser-through-selenium-using-firefoxprof

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!