I am working with Selenium, now there is a condition:
when I hit a button in my webpage a window pop up opens up.
Now I have to click a radio button (one out of two, it will work even if we send a TAB ) and then click an OK button. I searched in the net and got to know about "driver.getWindowHandle()".
But I don't have any idea dealing with the newly opened window popup.
Need help in this.
For switching purpose u can use enhanced for loop:
for (String winHandle : objDriver.getWindowHandles()) {
objDriver.switchTo().window(winHandle);
}
So it will switch the control from one driver window to child windows.
To interact with elements on the window try to find element with whatever tool u r using and perform the required action after switching to the window.
To return back to parent window you can use the same loop or use:
driver.switchTo().defaultContent();
Check my answer in this post and also read the comments to help you understand the difference between getWindowHandle() and getWindowHandles()
Java: focus is not on pop-window during window handling
We handled this situation using AutoItX - https://www.autoitscript.com/site/ in our Windows/IE C# project:
AutoItX3 autoIt = new AutoItX3();
var handle = autoIt.WinWaitActive("[window title]", "", 20);
Assert.IsTrue(handle != 0", string.Format("Was not able to find: {0}", [window title]);
autoIt.Send("{ESCAPE}"); // tab may work as well for selection
The pop up was a Windows window, and not part of IE, therefore the WebDriver didn't know about it.
Hope this helps.
Related
I am trying to automate a scenario where when I click on a link another tab opens with details.
Question 1 : Do I have to specifically set my focus to the 2nd tab or selenium automatically finds the element in the 2nd tab?
I am using the below code to set the focus to the 2nd tab :
String currentWindow = driver.getWindowHandle();
driver.switchTo().window(currentWindow);
Problem : I am getting an error that selenium is unable to find the specified element.
Could you guys suggest me what am I doing wrong, and the best way to switch to 2nd tab.
Actually, you are setting the focus on the first tab, not the second one. You need to do something like this
String currentWindow = driver.getWindowHandle();
// open the new tab here
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(currentWindow)) {
driver.switchTo().window(handle);
}
}
And the answer to your question is yes, you have to tell the driver to set its focus on the new tab.
You can get all the window handles as handlers=driver. GetWindowHandles() which will return all the handler string. Then using index switch to the appropriate handle using driver.switchto().window(handlers[1])
// opening the base URL
driver1.get(baseUrl+"/");
// opening a new tab
driver1.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND + "t");
driver1.get("my URL");
// getting back to the first tab
driver1.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND, Keys.SHIFT, "{");
// I want to signup by clicking the sign up button
driver1.findElement(By.xpath("/html/body/div[1]/header/div[2]/button")).click();
The error that I get after running is :
"Error communicating with the remote browser. It may have died."
But when I run the same code without the navigation, the button click works fine, then it means there is no problem with the xpath.
The information which browser you are using would be very interesting. And the webdriver doesn't need to control the visbile Tab. So i wouldn't let the Browser change the Tab and I would use the "switchTo" Method of the webdriver.
More information to this topic is here.
As #Kikkirej mentuined, i see no reason to use sendKeys to switch between opened tabs. Use Selenium instead, it is a much better approach.
Edit: in addition, try to provide more information, especially the most basic part - the browser you are automating.
I am running below java code for switching windows and getting error message, Kindly suggest something.
Driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
Set<String>set=Driver.getWindowHandles();
Iterator<String> It=set.iterator();
String PId=It.next();
String CId=It.next();
Driver.switchTo().window(CId);
Driver.get("https://www.facebook.com");/* Here again I want to come back to parent window and perform some action */
Please refer to this java documentation link for the list of available switchTo options:
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.TargetLocator.html
In your case, you may need to save the window handle before switching and then use it later to switch back to original window.
String originalWindow = Driver.getWindowHandle();
Driver.switchTo().window(CId);
//Operations on new window here
Driver.switchTo().window(originalWindow);
//Operations on original window here
I want to select the check box Prevent this page from creating additional dialogs before selecting ok to close the alert
Currently i am using
Alert alert=driver.switchTo().alert();
//check the checkbox
alert.accept();
As an normal interactive user to check the check box i have to use a combination of <Tab> + <Space/Enter> keys. The <Tab> shifts the focus to the check-box and the <Space/Enter> checks the check-box.
Solutions Tried
I tried using Java sendKeys mechanisms ( Robot class, driver.sendKeys(), etc.), but an UnhandledAlertException is getting thrown.
I tried using alert.sendKeys which is different from driver.sendKeys() but it too failed
//check the checkbox
alert.sendKeys("\t");
alert.sendKeys("{TAB}");
alert.sendKeys("\uE004");
alert.sendKeys("\\U+0009");
alert.sendKeys(Integer.toString(KeyEvent.VK_TAB));
I am trying to avoid robot class as much as possible as i need to run the test in grid in which case robot class will not work.Any pointers on how to send keys to the alert window ?
Temporarily i could do it using a javascript window.alert = function() {}; by simply overriding the alert with an empty function just curious to know if it could be done with webdriver functions like alert.sendkeys or any other methods ??
Any help is greatly appreciated!!
I am testing a web application that creates a new window long after a button is clicked. The sequence is the following
window 1: (parent window) click button to create window 2
window 2: progress window appears until background process on server returns data
window 3: progress window turns into 3rd window (with different handle)
I want to properly wait for the 3rd window to appear. I know what the 'title' of all 3 windows will be however in order to get the titles from WebDriver I have to use the following code:
while(timeout has not occured...){
for (String handle : _driver.getWindowHandles()) {
String myTitle = driver.switchTo().window(handle).getTitle();
if(3rdWindowTitle.equalsIgnoreCase(myTitle)){
return true;
}
}
}
This will effectively switch the active window back and forth every time it loops because of the 'switchTo'. This causes the firefox windows to cycle back and forth really quickly and is obnoxious. What I need is a way to get the title's of the windows that are available without having to 'switchTo' each window in a loop waiting for the 3rd window. Any ideas?
I basically want a method (waitForWindowByTitle(titleIWant)) which will block until the window with the title I want appears.
Well, Better you can wait for your window to appear by checking the number of windows. Like:
for(int i=0; i<noOfTrials;i++){
noOfWindows = driver.getWindowHandles().size();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if(noOfWindows>currentNoOfWindows){
break;
}
}
}
then for the first and last time you can browse through the windows (using switchTo) and navigate to the window you want.