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

前端 未结 28 2028
悲&欢浪女
悲&欢浪女 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:28

    Test step/scenario:
    1. Open a browser and navigate to TestURL
    2. Maximize the browser

    Maximize the browser with C# (.NET):

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

    Maximize the browser with Java :

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

    Another way to do with Java:

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension screenResolution = new Dimension((int) 
                        toolkit.getScreenSize().getWidth(), (int) 
                        toolkit.getScreenSize().getHeight());
    
    driver.manage().window().setSize(screenResolution);
    
    0 讨论(0)
  • 2020-11-29 18:28

    C# client drivers:

    driver = new FirefoxDriver(firefoxProfile);
    driver.Manage().Window.Size = new System.Drawing.Size(System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width+10, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height+10);
    

    ===> also add a reference to the .NET assembly "System.Windows.Forms"

    ... the only problem is that it's not positioned correctly
    ... please comment if you can correct this

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

    The below line of code would maximize IE, Chrome and Mozilla

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

    The above line of code and other workarounds mentioned in the post did not work for NodeWebKit browser, so as a workaround i had to use native C# code as mentioned below:

    public static void MaximiseNWKBrowser(IWebDriver d)
            {
                var body = UICommon.GetElement(By.TagName("body"), d);
                body.Click();
                string alt = "%";
                string space = " ";
                string down = "{DOWN}";
                string enter = "{ENTER}";
                SendKeys.SendWait(alt + space);
                for(var i = 1; i <= 6; i++)
                {
                    SendKeys.SendWait(down);
                }
                SendKeys.SendWait(enter);            
            }
    

    So this workaround basically uses "ALT+SPACE" to bring up the browser action menu to select "MAXIMIZE" from the options and presses "ENTER"

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

    Chrome driver already support:

    Java:

    webDriver = new ChromeDriver();
    webDriver.manage().window().maximize();
    
    0 讨论(0)
  • 2020-11-29 18:30

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

    This works for IE and Firefox. Chrome does not work. There is a bug submitted for this on ChromeDriver project.

    Meanwhile, the get around for the chrome is to implement what Joey V. and Coder323 suggested.

    ChromeOptions options = new ChromeOptions();
    options.addArgument("--start-maximized");
    driver = new ChromeDriver(options);
    
    0 讨论(0)
  • 2020-11-29 18:31

    You can try with this code to maximize chrome window.

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--window-size=1920,1080");
    
    WebDriver driver = new ChromeDriver(options);
    
    0 讨论(0)
提交回复
热议问题