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

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

    We have observed bug with new driver libraries. You can use slightly old jars which are able to handle new browsers versions.

    The main generic option is :-

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

    You can also use other option for maximizing the browser window.

    Example:-

    Add below option and pass it to driver:-

        chromeOptions.addArguments("--start-maximized");
    

    The full code will look like below :-

    System.setProperty("webdriver.chrome.driver","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\chromedriver.exe");
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--start-maximized");
    driver = new ChromeDriver(chromeOptions);
    

    OR

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    int Width = (int) toolkit.getScreenSize().getWidth();
    int Height = (int)toolkit.getScreenSize().getHeight();
    //For Dimension class, Import following library "org.openqa.selenium.Dimension"  
    driver.manage().window().setSize(new Dimension(Width,Height));
    driver.get("https://google.com");
    

    Try this on safari :-

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    String screenWidth = jse.executeScript("return screen.availWidth").toString();
    String screenHeight = jse.executeScript("return screen.availHeight").toString();
    int intScreenWidth = Integer.parseInt(screenWidth);
    int intScreenHeight = Integer.parseInt(screenHeight);
    Dimension d = new Dimension(intScreenWidth, intScreenHeight);
    driver.manage.window.setSize(d);
    
    0 讨论(0)
  • 2020-11-29 18:38

    For IE and Firefox:

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

    For Chrome:

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

    This option is fine for me :

    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-fullscreen");
    

    This option works on all OS.

    0 讨论(0)
  • 2020-11-29 18:40
    using System.Windows.Forms;
    using System.Drawing;
    
    public static void MaximizeBrowser(this IE myBrowser)
    {
        myBrowser.SizeWindow(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
    }
    

    I used Jim's code, but slightly modified for use with WatiN and C# to maximize the browser.

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

    Here's what worked for me in C#, firefoxDriver is global to the class:

    in the usings:

    using System.Drawing;
    using System.Windows.Forms;
    

    in the code:

    this.firefoxDriver = new FirefoxDriver();
    this.firefoxDriver.Manage().Window.Position = new Point(0, 0);
    this.firefoxDriver.Manage().Window.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
    
    0 讨论(0)
  • 2020-11-29 18:43

    You can use something like this (C#):

    driver.Manage().Window.Size = new Size(1024, 768);
    
    0 讨论(0)
提交回复
热议问题