Selenium Webdriver unable to give JavaScript popup window handle - javascript

I have one web page page where there is an button which when get clicked, popup an window form, where i have to select some values. I am currently using IE browser and selenium 2.53 libraries to automate this flow. Now my problem is that i am unable to switch to this popup window since i am not getting any window handle and every time it is showing only one parent handle.
I have tried alert/popup switch also but still this popup as invoked via a button from the parent page are not recognizable and so unable to do any action in this pop up page.
Set<String> winhandle= driver.getWindowHandles();
System.out.println(winhandle);
for (String handle : driver.getWindowHandles())
{
System.out.println(handle);
String newURL = driver.getTitle();
System.out.println(newURL);
}
Regards,
Nir

In your code you never actually iterate over the other windows.
In order to correctly print the handles of all the windows (including the popup), try to modify your code:
Set<String> winhandle = driver.getWindowHandles();
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle); // This line was missing. It tells the driver to operate on a different window
System.out.println(handle);
}
And then - after testing - you can just break the loop whenever you're in the right window.
Does this work for you?

Its strange but now true that same code using the window handles and switchTO is working fine with the chrome browser and it was not working for the IE browser. I don't know how to fix it in the IE browser but working great in the chrome browser.
regards,
Nir

Related

How can I stop Chrome blocking me setting a window.location after the user has changed the URL?

I open a new window to point at a page in a manual with var manual_win = window.open("", "manual");
I then set the location (after a bit of async code that tests the existence of the page) with manual_win.location = url;
This works and opens the manual page in a new named tab. I can later run the same code with a different URL and it works fine.
If the user edits the URL manually on the named tab to be some other site such as google.co.uk then the code no longer works on Chrome 76. It just brings the tab to the foreground and doesn't change the location. There is no issue with Firefox. I presume this is due to some sort of popup blocking logic in Chrome.
Is there any way to get around this?
It seems like a bug in Chrome that it blocks me from changing the location yet still brings it to the foreground. It would have made more sense for it to have cleared the name from the tab when it took control way from me so I end up opening a new tab instead.

javascript : window.open

I am working in vb.net 2005. I am in a position to start a new browser with process.start().
Now I have to open that browser in a specific size(say height:300 width:500)
Process.Start("firefox.exe", "abc.html")
and I have written this following code on load of abc.html
var myRef = window.open('abc.html','','left=20,top=20,width=300,height=500');
but it does not resize.
If I add 1 button on this page and click on it (by writing same code on its click event), a new window with expected size opens.
Am I going wrong somewhere?
Thanx.
Firefox doesn't let pages resize the window by default. Also note, if you already have Firefox running then browser preferences will dictate whether you get a new window or a tab. You can force a separate instance of Firefox by using the -no-remote command line flag, but then you won't be able to use the default profile (only one Firefox instance per profile).
My questions for you are:
Why are you launching Firefox from another executable at all instead of just having users click on a link and have it open in their default browser?
If you do need to launch Firefox from an executable, why spend all this effort overriding the user's preferences and settings?
If you' re launching from an executable and are keen to annoy your users whatever the cost, why not just find and resize the Firefox window using the normal Windows APIs?

window.open() still returns null

I am using IE8 on Windows 7. Referred to several threads and understand that in IE8 when I am using window.open to popup a new window, the JavaScript window.open is returning null value.
If I run IE as administrator or disable the protected mode, I see the window.open returns the expected object.
I am looking out for a solution apart from the options mentioned above. For such a small feature (opening a popup) I cannot ask customer to run IE as administrator or disable the protected mode.
If there is any work around, please let me know. It will be a great help.
Primarily, I want to make sure that only one window is opened when user clicks multiple times on the link and give the focus to the window which is already open. To achieve this I need to get the object from window.open so that I can check if the window is already open and give the focus to the already opened window. Otherwise open a new window.
For IE10, window.open returns a NULL reference object if Enable Protected Mode is checked under Internet Options->Security->Security Level for this zone and the ZONE is different i.e. in my case local file opening a popup from Intranet.
window.open returns a reference object even if Enable Protected Mode is checked when yoursite.com opens someothersite.com in popup window i.e. Internet->Internet
You can use window.showModalDialog as a alternative or replacement for window.open method.
It is more secure then window.open. It will not allow user clicking the Parent page.
Example Usage:
var myFeatures = "dialogWidth:1060px;dialogHeight:550px;resizable:yes";
window.showModalDialog(url,window,myFeatures);
//Here window is an object, no need to assign or declare.
If you want more detail explanation see Here.
//Fifth Question.

Open new browser windows in JavaScript without making it active

I have some JavaScript that makes an AJAX call and, if the call fails, opens a new windows (tab in Firefox) and displays the response from the server in that window. This is very convenient for debugging, because the error is typically from Pylons, so it's a full HTML page.
The only problem is that the new tab becomes the active tab, which would totally confuse a regular user. Is there any way to open the tab/window, but not make it active, ie. keep the current active window?
My code currently looks like this:
errorWindow = window.open("", "TCerrorWindow")
if (errorWindow)
errorWindow.document.write(xhr.responseText);
You can call errorWindow.blur(); window.focus(); after, forcing the browser to return focus to the previous window.
The effect you're trying to achieve is commonly called a pop-under window.
AFAIK this is not possible, as a security measure against pop-under windows. For debugging purposes you could
use Firebug (with a handy console, where you can output your own log messages from the code)
create a debug layer (div) on your page, where you output error messages in case an error happens

Is there a cross-browser method to bring a popup window into the foreground?

I have a chat in a popup browser window. This chat page (ASP) checks for new messages every 10 seconds.
I would like to bring this popup window to front when there is a new message. I tried with "window.focus()", but this work only in few version of IE.
Are there other options to achieve my goal?
Don't steal the focus.
And no, it will work only in some IEs, as you said. Quoting Mozilla's developer network,
It may fail due to user settings and
the window isn't guaranteed to be
frontmost before this method returns.
This is valid for all modern browsers, I guess.
You can use the following approach in order to notify the user of an activity.
Yes, there is a way out. You can use the JavascriptExecutor class to get the hidden browser and switch to it. This will bring the browser to the foreground.
String window = driver.getWindowHandle();
((JavascriptExecutor) driver).executeScript("alert('Test')");
driver.switchTo().alert().accept();
driver.switchTo().window(window);

Categories