Pulkit Agrawal Pulkit Agrawal - 2 months ago 9
Java Question

How to login on Goibibo website using selenium

I am trying to automate

Goibibo
Website in
Selenium
using
Java
. After Clicking on
Sign
in Tab, login pop up is displaying. How to switch to pop up so that I can enter details into the
Goibibo
Pop up. I wrote the following code:

public class Testclass1 {

public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "D://chromedriver_win32//chromedriver.exe");
WebDriver Driver = new ChromeDriver();
Driver.manage().window().maximize();
Driver.get("https://www.goibibo.com/");
Thread.sleep(5000);
//HANDLE THE POP UP
String handle = Driver.getWindowHandle();
System.out.println(handle);
// Click on the Button "New Message Window"
Driver.findElement(By.linkText("Sign In")).click();
Thread.sleep(3000);
// Store and Print the name of all the windows open
Set handles = Driver.getWindowHandles();
System.out.println(handles);
// Pass a window handle to the other window
for (String handle1 : Driver.getWindowHandles()) {
System.out.println(handle1);
Driver.switchTo().window(handle1);
}
Thread.sleep(3000);
Driver.findElement(By.name("username")).sendKeys("bittu.agrawal773@gmail.com");
//WAIT
}
}

Answer

Opened popup is not a new window popup, it's just simple HTML login popup present is iframe which can be simply handled by switching to iframe as below working code :-

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

driver.get("https://www.goibibo.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);

wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Sign In"))).click();

//switch to popup iframe to enter login credentials into form
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("authiframe"));

//now enter login credentials   
driver.findElement(By.id("id_username")).sendKeys("username");
driver.findElement(By.id("id_password")).sendKeys("password");

//now click on sign in button
driver.findElement(By.id("signinBtn")).click();

Note:- For better way, you should use WebDriverWait to wait explicitly for element with certain ExpectedConditions instead of Thread.sleep().

Comments