How to minimize the browser window in Selenium WebDriver 3

前端 未结 7 1021
南方客
南方客 2021-01-05 18:14

After maximizing the window by driver.manage().window().maximize();, how do I minimize the browser window in Selenium WebDriver with Java?

相关标签:
7条回答
  • 2021-01-05 18:23

    There seems to be a minimize function now:

    From the documentation on:

    webdriver.manage().window()

    this.minimize() → Promise<undefined>
    Minimizes the current window. The exact behavior of this command is specific to individual window managers, but typicallly involves hiding the window in the system tray.
    
    Parameters
    None.
    
    Returns
    Promise<undefined>
    A promise that will be resolved when the command has completed.
    

    So the code should be:

    webdriver.manage().window().minimize()
    

    At least in JavaScript.

    0 讨论(0)
  • 2021-01-05 18:27

    You have not mentioned your development technology. I am using C#.

    With this, there is no way available to minimize browser window. This is for security reasons may be. You can maximize window though as you mentioned in question.

    But, you can set the position of window with following code:

    driver.Manage().Window.Position = new System.Drawing.Point(x, y);
    

    In Java, it may be something like follows; not sure about syntax:

    driver.manage().window().setPosition(new org.openqa.selenium.Point(x, y));
    

    Refer this and this.

    With this input, you can set x and y with respect to your operating system resolution values, that should hide the browser.

    0 讨论(0)
  • 2021-01-05 18:30

    Use the following code to the minimize browser window. It worked for me and I am using Selenium 3.5:

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_SPACE);
    robot.keyPress(KeyEvent.VK_N);
    robot.keyRelease(KeyEvent.VK_ALT);
    robot.keyRelease(KeyEvent.VK_SPACE);
    robot.keyRelease(KeyEvent.VK_N);
    
    0 讨论(0)
  • 2021-01-05 18:34

    Unfortunately, Selenium does not provide any built-in function for minimizing the browser window. There is only the function for maximizing the window. But there is some workaround for doing this.

    driver.manage().window().setPosition(new Point(-2000, 0));
    
    0 讨论(0)
  • 2021-01-05 18:35

    I'm using Selenium WebDriver (Java) 3.4.0 and have not found the minimize function either. Then I'm using the following code.

    driver.manage().window().maximize();
    driver.manage().window().setPosition(new Point(0, -2000));
    

    We need to import the following for the Point function.

    import org.openqa.selenium.Point;
    
    0 讨论(0)
  • 2021-01-05 18:38

    Selenium's Java client doesn't have a built-in method to minimize the browsing context.

    However, as the default/common practice is to open the browser in maximized mode, while Test Execution is in progress minimizing the browser would be against the best practices as Selenium may lose the focus over the browsing context and an exception may raise during the test execution. However, Selenium's Python client does have a minimize_window() method which eventually pushes the Chrome browsing context effectively to the background.


    Sample code

    Python:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.add_argument("--start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get('https://www.google.co.in')
    driver.minimize_window()
    

    Java:

    driver.navigate().to("https://www.google.com/");
    Point p = driver.manage().window().getPosition();
    Dimension d = driver.manage().window().getSize();
    driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
    
    0 讨论(0)
提交回复
热议问题