How to maximize the browser window in Selenium WebDriver (Selenium 2) using C#?

前端 未结 28 2027
悲&欢浪女
悲&欢浪女 2020-11-29 18:20

Is there any way to maximize the browser window using WebDriver (Selenium 2) with C#?

相关标签:
28条回答
  • 2020-11-29 18:32

    This is working fine for me.

    Capybara.current_session.driver.browser.manage.window.resize_to(1800, 1000)
    
    0 讨论(0)
  • 2020-11-29 18:32

    I tried many of the answers above, but none work well. My chrome driver version is 2.7 and Iam using selenium-java vesion is 2.9.0. The official document suggests using:

    var capabilities = new DesiredCapabilities();
    var switches = new List<string>
                           {
                               "--start-maximized"
                           };
    capabilities.SetCapability("chrome.switches", switches);    
    new ChromeDriver(chromedriver_path, capabilities);
    

    The above also does not work. I checked the chrome driver JsonWireProtocol: http://code.google.com/p/selenium/wiki/JsonWireProtocol

    The chrome diver protocol provides a method to maximize the window:

    /session/:sessionId/window/:windowHandle/maximize,
    

    but this command is not used in selenium-java. This means you also send the command to chrome yourself. Once I did this it works.

    0 讨论(0)
  • 2020-11-29 18:34

    For me, none of the solutions above worked when working with Selenium Web Driver C# + Chrome:

    • window.resizeTo(1024, 768); - I want to use the whole screen
    • --start-maximized - it is ignored
    • driver.manage().window().maximize(); - does not work because it requires some extension and I am not allowed to use Chrome extensions

    I managed to get it working using InputSimulator:

    var inputSim = new InputSimulator();
    // WinKey + UP = Maximize focused window
    inputSim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.UP);
    
    0 讨论(0)
  • 2020-11-29 18:35

    Simply use Window.Maximize() command

    WebDriver driver= new ChromeDriver()
    driver.Manage().Window.Maximize();  
    
    0 讨论(0)
  • 2020-11-29 18:35

    For C#:

    driver.Manage().Window.Maximize();
    

    For Java:

    driver.manage().window().maximize();
    
    0 讨论(0)
  • 2020-11-29 18:36

    For Java:

    driver.manage().window().maximize();
    

    It will work in IE, Mozilla, Chrome

    0 讨论(0)
提交回复
热议问题