Handling Multiple window in selenium web driver

154 views
Skip to first unread message

Ram Prasath

unread,
Sep 15, 2016, 11:42:42 AM9/15/16
to Selenium Users

Hi all,

I'm getting below error when we switch from one window to another window in firefox and i can't able to locate web element in new window please help me on this!!!!!!


Expected result : 

Need to locate web element in new window and perform action 



[TestNG] Running:
C:\Users\apptivo\AppData\Local\Temp\testng-eclipse-665293667\testng-customsuite.xml

Amazon.in: moto g4 plus - Smartphones / Smartphones & Basic Mobiles: Electronics
{6c69c5eb-b633-489d-b379-d364b5ac2693}
PASSED: SearchItem
FAILED: SelectItem
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'DESKTOP-PETKCBJ', ip: '10.80.129.188', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_102'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:353)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:490)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:345)
at amaz.AmTest.SelectItem(AmTest.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:821)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:773)
at org.testng.TestRunner.run(TestRunner.java:623)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
at org.testng.TestNG.run(TestNG.java:1018)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 127.0.0.1:7057 [/127.0.0.1] failed: Connection refused: connect
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:161)
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:89)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:170)
at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:380)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:620)
... 29 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)

anemuday

unread,
Sep 15, 2016, 11:22:31 PM9/15/16
to Selenium Users
I hope you know this and already tried, but let you aware if you dont know.

Inorder to interact with any new window first you need to switch to that window.

Each window is identified with a handler. You can get this handler by 
String strWindowHandler=driver.getWindowHandle(); //Current opened browser window handle

Once you get the handle, you to set focus/point your driver to this window.
You can do this by using:
driver.switchTo().Window(strWindowHandler)

To switch between windows you need to use
Set<String> strWindows=driver.getWindowHandlers();

Iterate through each window and act on your required browser window.

Without the above you cannot switch to new window and cannot access any controls on it.

Find below video may help for detailed explanation.

Thanks,
Uday

Shubham Agarwal

unread,
Sep 26, 2016, 6:08:33 AM9/26/16
to Selenium Users

Just add this , when u want to handle the other window.


for(String winHandle : driver.getWindowHandles()){

    driver.switchTo().window(winHandle);

⇜Krishnan Mahadevan⇝

unread,
Sep 26, 2016, 6:11:39 AM9/26/16
to Selenium Users
Shubham
You shared a code snippet that basically is iterating through a set of windows within a loop. What is that going to solve ?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/e5a6dafb-80f4-421b-b024-1ff392565398%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shubham Agarwal

unread,
Sep 26, 2016, 6:15:33 AM9/26/16
to Selenium Users
Hi Krishnan,

whenever you want to switch on new window , it will handle the new window and focused on that , then you can perform further operations on new window.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

⇜Krishnan Mahadevan⇝

unread,
Sep 26, 2016, 6:18:39 AM9/26/16
to Selenium Users
Please take a closer look at your code snippet.

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

Lets say getWindowHandles() returns back 12 window handles but you actually had to switch only to the second window.
Now tell me, does your code switch to the second window handle and let me proceed with the next operation (or) does it just switch between all the 12 windows (till the loop ends) and then let me do some operation ?




Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/e27a6317-3b32-4e5a-bb69-0ab16f184194%40googlegroups.com.

Shubham Agarwal

unread,
Sep 26, 2016, 6:25:10 AM9/26/16
to Selenium Users
I got your point , basically this code will not work on handling multiple windows , like you are giving the example of 12 windows.
if you want to handle multiple windows then use this code :

Set < String > s = driver.getWindowHandles(); 

driver.switchTo().window(s.toArray()[0].toString());

// Any operation

driver.close();

driver.switchTo().window(s.toArray()[1].toString());

// Any Operation

driver.close();

driver.switchTo().window(s.toArray()[2].toString());

// Any operation 

This is the best solution , I ever used to handle multiple windows :)

Jim Evans

unread,
Sep 26, 2016, 8:40:54 AM9/26/16
to Selenium Users
Except that you are assuming there's an order to the handles returned by getWindowHandles(). It's a common mistake, but the handles are explicitly not in any order. In other words, if getWindowHandles() returns N handles, the first handle in the list (handles.get(0)) is not necessarily the first browser window opened, nor is the last handle in the list (handles.get(N - 1)) the most recently opened window. You need to be a little more sophisticated to make sure you're selecting the right window.

Shubham Agarwal

unread,
Sep 26, 2016, 9:04:11 AM9/26/16
to Selenium Users
Hi Jim, 

That's an interesting fact , even I have not tried this , so may you please tell me how we can handle in this situation ?

Andreas Tolfsen

unread,
Sep 26, 2016, 11:30:31 AM9/26/16
to seleniu...@googlegroups.com, Shubham Agarwal
First of all, thanks to Jim for his excellent explanation.

Shubham Agarwal <saga...@astegic.com> writes:

> That's an interesting fact , even I have not tried this , so may you
> please tell me how we can handle in this situation ?

You will either need to keep track of the windows opened yourself so
you can provide a somewhat index-safe list, and to do this you likely
need to apply a diffing technique to what the most recent introduced
window is.

Example in Python code (untested):

original_handles = set(driver.window_handles)

def open_link_in_window(driver):
diff = set(driver.window_handles) - original_handles
if len(diff) > 0:
new_window = diff[0]
driver.switch_to_window(new_window)

is_loaded = driver.execute_script(
"return document.readyState == 'DOMContentLoaded'")
return is_loaded

link = driver.find_element_by_tag_name("a")
link.click()
Wait(driver).until(open_link_in_window)
Reply all
Reply to author
Forward
Message has been deleted
0 new messages