如何使用Java在Selenium WebDriver中处理popup窗口
我想在rediff.com中处理login部分,但下面的代码不适用于此:
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");
如果myWindowHandle
不是正确的string,那么让我知道如何得到popup窗口名称,因为我找不到popup窗口的名称。
要切换到popup窗口,您需要使用getWindowHandles()
并遍历它们。
在你的代码中,你正在使用getWindowHandle()
,它会给你父窗口本身。
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
我find了上述程序的解决scheme,其目标是login到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(); } }
您可以处理popup窗口或警报框:
Alert alert = driver.switchTo().alert(); alert.accept();
您也可以拒绝提示框:
Alert alert = driver.switchTo().alert(); alert().dismiss();
当您得到任何Web浏览器popup警报消息框时,您可以在代码中使用下面的代码。
// Accepts (Click on OK) Chrome Alert Browser for RESET button. Alert alertOK = driver.switchTo().alert(); alertOK.accept(); //Rejects (Click on Cancel) Chrome Browser Alert for RESET button. Alert alertCancel = driver.switchTo().alert(); alertCancel.dismiss();
不要让情况变得复杂。 如果可用,请使用ID
。
driver.get("http://www.rediff.com"); WebElement sign = driver.findElement(By.linkText("Sign in")); sign.click(); WebElement email_id= driver.findElement(By.id("c_uname")); email_id.sendKeys("hi");
当fireas的消息在firefox的屏幕上猛增的时候。 下面的标签显示在火虫中。
<div class="toast-message">Invalid Credentials, Please check Password</div>.
我当时拿了截图。 并做了以下更改在seleniumjava代码。
String alertText = ""; WebDriverWait wait = new WebDriverWait(driver, 5); wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("toast-message"))); WebElement toast1 = driver.findElement(By.className("toast-message")); alertText = toast1.getText(); System.out.println( alertText);
而我的问题托pipepopup得到解决。
//get the main handle and remove it //whatever remains is the child pop up window handle String mainHandle = driver.getWindowHandle(); Set<String> allHandles = driver.getWindowHandles(); Iterator<String> iter = allHandles.iterator(); allHandles.remove(mainHandle); String childHandle=iter.next();
public void Test(){ WebElement sign = fc.findElement(By.xpath(".//*[@id='login-scroll']/a")); sign.click(); WebElement LoginAsGuest=fc.findElement(By.xpath(".//*[@id='guest-login-option']")); LoginAsGuest.click(); WebElement email_id= fc.findElement(By.xpath(".//*[@id='guestemail']")); email_id.sendKeys("ankushdeoladore@gmail.com"); WebElement ContinueButton=fc.findElement(By.xpath(".//*[@id='contibutton']")); ContinueButton.click(); }