Reputation: 181
If the browser window is not in focus, all webdriver identifications on the current page fail.
How can the browser be brought into focus, using webdriver?
Upvotes: 18
Views: 47105
Reputation: 1
(Python solution) This seems to perform consistently for Chrome and snatches focus and the foreground window layer without resizing the window.
driver.minimize_window()
driver.switch_to.window(driver.current_window_handle)
Upvotes: 0
Reputation: 107
This works for me (in python):
driver.minimize_window()
driver.maximize_window()
Upvotes: 0
Reputation: 1
Setting the focus at the "driver options" should work:
InternetExplorerOptions options = new InternetExplorerOptions();
options.setCapability("requireWindowFocus", true);
Upvotes: 0
Reputation: 30957
executeScript("window.focus();")
didn't work for me in the latest Chrome (v47 at the time of this post)
However I found a hack in another question which does work in this version of Chrome.
Here are the generic steps, since the question doesn't specify the Selenium API language:
Implementation in webdriverjs
, which I'm using
const chrome = setupChromeWebdriver(); // get your webdriver here
chrome.executeScript('alert("Focus window")'))
.then(() => chrome.switchTo().alert().accept());
Upvotes: 9
Reputation: 692
This works for me. After the code that opens the browser, issue this snippet:
String window = driver.getWindowHandle();
((JavascriptExecutor) driver).executeScript("alert('Test')");
driver.switchTo().alert().accept();
driver.switchTo().window(window);
Upvotes: 3
Reputation: 6963
Selenium 3.0 takes this:
((IJavaScriptExecutor)po.WebDriver).ExecuteScript("window.focus();");
Upvotes: 1
Reputation: 7624
((JavascriptExecutor) webDriver).executeScript("window.focus();");
should do the trick!
Upvotes: 6