How to bring browser to front

365 views
Skip to first unread message

Snehal Biche

unread,
Jul 22, 2016, 1:19:31 AM7/22/16
to webdriver
I have two browsers one IE and another Chrome which will be opened by two diff. Webdriver instances. I am trying to find easy way to perform below actions -
1) Open IE browser 
2) Perform some action
3) Open Chrome browser
4) Perform some action
5) switch to IE browser
6) Perform some action
7) switch to Chrome browser
8) Perform some action
9) close both browser

Thanks,
Snehal

darrell grainger

unread,
Jul 22, 2016, 8:27:05 AM7/22/16
to webdriver
You just need two instances of WebDriver. Normally one would have:

WebDriver driver = new ChromeBrowser();

and all interaction with driver would go to the Chrome browser. In your situation I would have:

WebDriver chrome = new ChromeBrowser();
WebDriver ie = new InternetExplorer();

There will obviously be more to setting up each browser instance but that is the general idea. Now if I use chrome it will send commands to the Chrome browser instance, e.g.

chrome.get("http://www.google.com");

and any use of ie would send commands to Internet Explorer, e.g.


So there is no command to 'switch' to a different browser. Just using the different variable causes you to 'switch' to the other browser. Closing both browsers is really just close one browser then close the other browser, e.g.

ie.quit();
chrome.quit();

Darrell 

praveen kumar cherukuri

unread,
Jul 22, 2016, 9:33:18 AM7/22/16
to webd...@googlegroups.com
we cannot switch the focus between two different browsers (which are invoked by two different threads). 

One good tip for you to solve this issue is to share the complete screen to both these browser instances. 

Set the browser size and positions soon after you launch the browser. 
So, Ideally I mean to say, cascade the windows on the screen. (one to the right half of the screen and one to the left half of the screen). 

Would love to know if there is any other way to handle this (Special/Unique) requirement.

/Praveen. 



--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at https://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

Snehal Biche

unread,
Jul 23, 2016, 1:28:13 AM7/23/16
to webd...@googlegroups.com
@Darrell,

I have already tried that but I do get below exception if i try to interact with browser -

Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state

@Praveen,
Yes this is unique requirement but I think i have found the answer, I am going to use Autoit Script which has function to bringwindoToFront and bringWindowToBack based on . If that helps will post the result in thread.

my code below -
================

package gridTest;


import java.io.File;
import java.util.Hashtable;
import java.util.Map;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;



public class TwoBrowserTeset {

public static void main(String[] args) throws InterruptedException {
//chrome configuration started
        File file = new File(System.getProperty("user.dir") + "\\src\\setupEnv\\" +"chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
        //System.setProperty("Always.ask.where.to.save.files", true);
        Map<String, String> prefs = new Hashtable<String, String>();
        prefs.put("download.prompt_for_download", "true");
        prefs.put("download.default_directory", "C:\\");
        prefs.put("download.extensions_to_open", "pdf");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("chrome.prefs", prefs);
           

        WebDriver wb1= new ChromeDriver(capabilities);
        //done for chrome
        
        
        File fileIE = new File(System.getProperty("user.dir") + "\\src\\setupEnv\\" +"IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", fileIE.getAbsolutePath());
        DesiredCapabilities capsIE = DesiredCapabilities.internetExplorer();
        capsIE.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
        capsIE.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
        
        WebDriver wb2 = new InternetExplorerDriver(capsIE);
        
        
                
        //open site in Chrome
wb1.get("http://google.com");
wb1.manage().window().maximize();
//String ChromeWindowHandle = wb1.getWindowHandle();
Thread.sleep(30);
//open site in IE
//String IEWindowHandle = wb2.getWindowHandle();
Thread.sleep(30);
//do operations on Chrome
wb1.findElement(By.xpath("//*[@id='gs_htif0']")).sendKeys("selenium");
Thread.sleep(10);
Thread.sleep(30);
Thread.sleep(30);
wb2.findElement(By.xpath("//*[@id='alertexamples']")).click();
wb2.switchTo().alert().accept();
wb2.close();
wb1.close();
}

}
==============



Thanks,
Snehal

--
You received this message because you are subscribed to a topic in the Google Groups "webdriver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webdriver/hgHFLPkX_3o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webdriver+...@googlegroups.com.

To post to this group, send email to webd...@googlegroups.com.
Visit this group at https://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.



--
------------------------------------------------------------------
Warm Regards,
Snehal Biche
------------------------------------------------------------------

darrell grainger

unread,
Jul 23, 2016, 12:28:55 PM7/23/16
to webdriver
Your problem has absolutely nothing to do with instantiating two instances of WebDriver. You have the following line:
 
wb1.findElement(By.xpath("//*[@id='gs_htif0']")).sendKeys("selenium");

You are interacting with the hidden input on the Google Search page. This is guaranteed to issue org.openqa.selenium.InvalidElementStateException. Change this line to:

wb1.findElement(By.xpath("//input[@name='q']")).sendKeys("selenium");
 
This will fix your error. There are also many other issues I have with your code. You should read http://jimevansmusic.blogspot.ca/2012/08/youre-doing-it-wrong-protected-mode-and.html. To quote Simon Stewart:

We named the capability "INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS" for a very good reason: your tests will exhibit random hangs and flakiness when you attempt to cross domains when using it.

Darrell

Snehal Biche

unread,
Jul 27, 2016, 7:07:34 AM7/27/16
to webd...@googlegroups.com
@Darrell 

Thanks a lot for your inputs, Yes you are absolutely right.
My problem was due to 32 bit OS and I was using 64 bit IE driver. 

-Snehal
Reply all
Reply to author
Forward
0 new messages