我正在尝试使用Goibibo
在Selenium
中自动化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
}
}
答案 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()
的元素。