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

时间:2019-02-07 14:42:46

标签: 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)

options = Options()
options.some_optins = my_options

最后:

driver = Webdriver.Firefox(firefox_profile=profile, options=options) 

1 个答案:

答案 0 :(得分:1)

您看对了。

set_window_position()

set_window_position()设置当前窗口的xy位置。

  • 实施:

    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)
    
  • 定义:

    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与属于浏览器的窗口句柄耦合,并且只能由 webdriver 实例处理。

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

相关问题