简体   繁体   中英

Switch to Firefox browser using Selenium

Firefox browser only launches on Dock on my Mac but active screen shown is still Eclipse.

How can the focus be shifted to Browser?

public class FirefoxFirst {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.gecko.driver","/Users/varunnadimpalli/Downloads/geckodriver");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://google.com");

selenium :3.3.1
Mac:`10.12.1

Strange why it wouldn't swap focus to firefox, if it is not in focus but still open try

((JavascriptExecutor) webDriver).executeScript("window.focus();");

If it is minimized you can try going through the windows handles

for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

See if that is able to bring the firefox window up.

Not sure whether this works. Try maximizing the browser,

driver.manage().window().maximize();

Also, don't forget to add implicit wait after initiating the browser.

Note - I don't have enough points to comment this. else i have commented instead of writing this as answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM