Click OK on alert Selenium Webdriver

我的未来我决定 提交于 2019-12-12 17:40:33

问题


Ok so I know there are a number of other answers on this question about alerts in webdriver and I've looked through them but I think my situation is a little different. When I click my submit button I have already switched into 3 frames and then I get the alert, so I tried to switch back to default content and then click the alert using a try catch and alert.accept but it still doesn't click the alert. Code is below. Thanks in advance for your assistance :)

public class BookAHoliday {

    public FirstPage completeHolidayFormAndSubmit(String firstDate, String lastDate) {

        sleepsAreBad();
        driver.switchTo().frame("ContainerFrame");
        driver.switchTo().frame("iframeCommunityContainer");
        driver.switchTo().frame("FORMCONTAINER");
        fluentWait(By.id("StartDate_txtInput"));
        firstDayOfLeaveInput.sendKeys(firstDate);
        sleepsAreBad();
        lastDayofLeaveInput.sendKeys(lastDate);

        try {
            submitButton.click();
        } catch (UnhandledAlertException f) {
            try {
                sleepsAreBad();
                driver.switchTo().defaultContent();
                Alert alert = driver.switchTo().alert();
                String alertText = alert.getText();
                System.out.println("Alert data: " + alertText);
                alert.accept();
            } catch (NoAlertPresentException e) {
                e.printStackTrace();
            }
        }
        sleepsAreBad();

        return PageFactory.initElements(driver, FirstPage.class);
    }


    private void sleepsAreBad() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


public class BaseTest {


    public static WebDriver driver;
    static String driverPath = "C:\\";

    @BeforeClass
    public static void setUp() {
        System.out.println("****************");
        System.out.println("launching Browser");
        System.out.println("****************");
        // Browser selection

        //Firefox

        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
        driver = new FirefoxDriver(dc);

driver.get(URL);

 @AfterClass()
    public static void tearDown() {
        if (driver != null) {
            System.out.println("Closing browser");
            driver.quit();
        }

    }


public class Bookings extends BaseTest{

    @Test(description = "Holiday booking")
    public void CD01() {

        FirstPage firstPage = PageFactory.initElements(driver, FirstPage.class);
        firstPage


                 .logIn("username", "password")
                .clickHolidayLink()
                .completeHolidayFormAndSubmit("12/05/2016", "15/05/2016");



    }

alert box Here is the alert box


回答1:


Try this when you have got UnhandledAlertException in catch

WebDriverWait wait = new WebDriverWait(driver, 3000);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
alert.accept();

May be it will help you...:)




回答2:


For most alerts you can press enter to press alert`s "ok".



来源:https://stackoverflow.com/questions/37588384/click-ok-on-alert-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!