如何选中弹出窗口中的复选框? Selenium Webdriver

时间:2014-05-28 08:45:43

标签: selenium-webdriver netbeans-7

当弹出窗口打开时如何处理此窗口?

driver.switchTo().defaultContent();
        MenuUtil.doClickMenu(selenium, driver, "Access 2G", "Cảnh báo", "Giám sát cảnh báo BTS");
        ControlBase.waitForLoadOk(5);
        CheckBoxUtil.doCheckByXpath(selenium, "//*[@id='listRow1']/tbody/tr[1]/td[18]/input");
        ControlBase.waitForLoadOk(5);
        ButtonUtil.doClickByLabel(driver, "Cập nhật");
        ControlBase.waitForLoadOk(5);
        CheckBoxUtil.doCheckByXpath(selenium, "//*[@id='txtName']");

2 个答案:

答案 0 :(得分:0)

通常我使用Selenium IDE。在这里你可以'点击&记录',它允许您获得JS弹出警报框的XPath。

我建议您先检查一下警告框,其中显示的内容如下:

if (selenium.isAlertPresent()){

     System.out.println(selenium.getAlert()); //show alert text message
     selenium.click("id=alertbox_ok_btn"); //click OK on alert box         
}

从这里下载Selenium IDE:http://docs.seleniumhq.org/download/

另外,请参阅Selenium的Java文档:http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html

答案 1 :(得分:0)

尝试下面用JAVA编写的代码

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("Your application URL");

String parentWindow = driver.getWindowHandle();
//click on the link/box/whatever to open the pop up
driver.findElement(By.xpath("")).click();

Set <String> popupWindows = driver.getWindowHandles();
    for(String popup : popupWindows){
driver.switchTo().window(popup);
    //you can use getTitle/getCurrentUrl to match with title/url of the pop up
if(driver.getTitle().equals("actual title on the pop up")){
            break;
        }
    }

//Write a code to check a checkbox
//Close the pop up
driver.close();
//Come back to the parent window
driver.switchTo().window(parentWindow);
//Code if you want to test/do something else
driver.close();