Make sure the browser is open. Webdriver is always in focus.

If the browser window is not in focus, all the webdriver identifiers on the current page do not work.

How can the browser be focused using webdriver?

+10
source share
5 answers
((JavascriptExecutor) webDriver).executeScript("window.focus();"); 

gotta do the trick!

+7
source

executeScript("window.focus();") did not work for me in the latest version of Chrome (v47 at the time of publication)

However, I found a hack in another question that works in this version of Chrome.

Here are the general steps since the Selenium API language is not specified in the question:

  1. Show warning by running script in browser
  2. Receive alert

The implementation in webdriverjs that I use

 const chrome = setupChromeWebdriver(); // get your webdriver here chrome.executeScript('alert("Focus window")')) .then(() => chrome.switchTo().alert().accept()); 
+6
source

This works for me. After the code that the browser opens, enter this snippet:

 String window = driver.getWindowHandle(); ((JavascriptExecutor) driver).executeScript("alert('Test')"); driver.switchTo().alert().accept(); driver.switchTo().window(window); 
+2
source

Selenium 3.0 accepts this:

 ((IJavaScriptExecutor)po.WebDriver).ExecuteScript("window.focus();"); 
+1
source

Setting the focus to β€œdriver parameters” should work:

 InternetExplorerOptions options = new InternetExplorerOptions(); options.setCapability("requireWindowFocus", true); 
0
source

All Articles