如何使用selenium登录Goibibo网站

时间:2016-09-12 09:54:43

标签: java selenium selenium-webdriver

我正在尝试使用GoibiboSelenium中自动化Java网站。单击选项卡中的Sign后,将显示登录弹出窗口。如何切换到弹出窗口,以便我可以在Goibibo弹出窗口中输入详细信息。我写了以下代码:

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
    }
}

1 个答案:

答案 0 :(得分:0)

已打开的popup不是新窗口popup,只是简单的HTML登录弹出窗口iframe可以通过切换到iframe来简单处理,如下工作代码: -

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();

注意: - 为了更好的方法,您应该使用WebDriverWait明确等待具有特定ExpectedConditions而非Thread.sleep()的元素。