I want to handle sign-in part in rediff.com, but the below code doesn't work for that:
driver.get("http://www.rediff.com/");
WebElement sign = driver.findElement(By.xpath("//html/body/div[3]/div[3]/span[4]/span/a"));
sign.click();
String myWindowHandle = driver.getWindowHandle();
driver.switchTo().window(myWindowHandle);
WebElement email_id= driver.findElement(By.xpath("//*[@id='signin_info']/a[1]"));
email_id.sendKeys("hi");
If myWindowHandle
is not the correct string, then let me know how to get the pop-up Window name, because I can't find the name of the pop-up window.
Yes, it is possible to handle Windows based pop-ups in Selenium webdriver. Sometimes on clicking a link or a button, another window gets opened. It can be a pop up with information or an advertisement. The methods getWindowHandles and getWindowHandle are used to handle child windows.
In this scenario, we will consider using our own Edureka demo site to illustrate how to handle alerts in Selenium Webdriver. Launch the web browser and open the webpage. Click on the button, “Display Alert” which generates the alert box and then click on OK(accept) button.
As we know, whenever we are executing any of the automation scripts using Selenium WebDriver, the WebDriver always has the focus on the main browser window and will run all the commands on the main browser window only. But, whenever an alert/popup appears, it opens up a new window.
In Selenium web driver there are methods through which we can handle multiple windows. To handle all opened windows by web driver, we can use "Driver.getWindowHandles ()" and then we can switch window from one window to another in a web application. Its return type is Iterator<String>.
The window Id can be fetched from Window Handler API available in selenium. it uniquely identifies the address of all the windows open at a time. Below are the methods to handle the windows: This function allows the handling of the current window by getting the current window Handle. It returns the type String.
We can handle authentication popup with Selenium. To do this, we have to pass the user credentials within the URL. We shall have to add the username and password to the URL. https://username:password@URL https://admin:admin@the−nternet.herokuapp.com/basic_auth Here, the admin is the username and password.
To switch to a popup window, you need to use getWindowHandles()
and iterate through them.
In your code you are using getWindowHandle()
which will give you the parent window itself.
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
// Now you are in the popup window, perform necessary actions here
driver.switchTo().window(parentWindowHandler); // switch back to parent window
I found the solution for the above program, which had the goal of signing in to http://rediff.com
public class Handle_popupNAlert
{
public static void main(String[] args ) throws InterruptedException
{
WebDriver driver= new FirefoxDriver();
driver.get("http://www.rediff.com/");
WebElement sign = driver.findElement(By.xpath("//html/body/div[3]/div[3]/span[4]/span/a"));
sign.click();
Set<String> windowId = driver.getWindowHandles(); // get window id of current window
Iterator<String> itererator = windowId.iterator();
String mainWinID = itererator.next();
String newAdwinID = itererator.next();
driver.switchTo().window(newAdwinID);
System.out.println(driver.getTitle());
Thread.sleep(3000);
driver.close();
driver.switchTo().window(mainWinID);
System.out.println(driver.getTitle());
Thread.sleep(2000);
WebElement email_id= driver.findElement(By.xpath("//*[@id='c_uname']"));
email_id.sendKeys("hi");
Thread.sleep(5000);
driver.close();
driver.quit();
}
}
You can handle popup window or alert box:
Alert alert = driver.switchTo().alert();
alert.accept();
You can also decline the alert box:
Alert alert = driver.switchTo().alert();
alert().dismiss();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With