Make sure that browser opened by webdriver is always in focus
Asked Answered
D

6

18

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?

Deodar answered 18/5, 2012 at 8:59 Comment(3)
What browser? Firefox, IE, Chrome?Bedevil
Why wouldn't the instance have focus? If you're trying to operate normally on the computer running the tests, then yeah, that won't work well since Selenium uses OS native keyboard and click instructions that you would interfere with. Sometimes, driver.switchTo().defaultContent() (and/or switchTo().activeElement()) helps - mostly on IE. What's your use case?Renounce
The process launching the browser is sometimes retaining focus - this appears to interfere with the browser not rendering because it idles invisible tabs by design if another window covers the entire browser tab.Leolaleoline
J
9

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:

  1. Show alert by executing script in browser
  2. Accept the alert

Implementation in webdriverjs, which I'm using

const chrome = setupChromeWebdriver(); // get your webdriver here

chrome.executeScript('alert("Focus window")'))
  .then(() => chrome.switchTo().alert().accept());
Jonette answered 22/12, 2015 at 23:54 Comment(0)
S
6
((JavascriptExecutor) webDriver).executeScript("window.focus();");

should do the trick!

Spud answered 22/5, 2012 at 10:50 Comment(2)
+1. So simple. Saves me writing a very convoluted AHK script.Sula
I am using internet explorer 9. Doesn't work for me.Delmardelmer
W
3

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);
Warrantor answered 3/3, 2018 at 0:37 Comment(1)
Had no effect using this combined strategy of both methods on Chrome v 87.0 (Windows on a Selenium grid to a local browser)Leolaleoline
D
1

Selenium 3.0 takes this:

((IJavaScriptExecutor)po.WebDriver).ExecuteScript("window.focus();");
Dispose answered 10/11, 2016 at 22:7 Comment(1)
This doesn't mean browser window will get actual focus, i.e. it doesn't guarantee browser window will be active on OS level; there are cases where this will only trigger OS level attention message.Socialite
P
0

Setting the focus at the "driver options" should work:

InternetExplorerOptions options = new InternetExplorerOptions();
options.setCapability("requireWindowFocus", true);
Protagoras answered 8/4, 2019 at 15:12 Comment(0)
L
0

This works for me (in python):

driver.minimize_window()
driver.maximize_window()
Lindyline answered 13/6, 2023 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.