问题
After maximizing window by driver.manage().window().maximize();
how to minimize browser window in selenium webdriver with java
回答1:
Unfortunately, Selenium does not provide any built-in function for minimizing the browser window, there is the only function for maximizing the window. But there is some workaround for doing this.
driver.manage().window().setPosition(new Point(-2000, 0));
Please check with this.
回答2:
This is a quite old question. But there seems to be a minimize function now:
From the Docs 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.
回答3:
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.
回答4:
Use following code to 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);
回答5:
I'm using Selenium Web Driver (Java) 3.4.0 and not found the minimize function, too. Then I'm using following code.
driver.manage().window().maximize();
driver.manage().window().setPosition(new Point(0, -2000));
** We need to import following for the Point function.
import org.openqa.selenium.Point;
回答6:
Selenium's java client doesn't have a built-in method to minimize the Browsing Context. Minimizing the browser while the Test Execution is In Progress would be against the best practices as Selenium may loose 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())));
来源:https://stackoverflow.com/questions/42647058/how-to-minimize-browser-window-in-selenium-webdriver-3