Selenium WebDriver with Java: Can't accept alert

前端 未结 3 1577
忘掉有多难
忘掉有多难 2020-12-18 14:58

When recording in selenium IDE I can click the \"OK\" button in a popup, and expected to be able to click it using

driver.findElement(By.linkText(\"OK\")).cl         


        
3条回答
  •  Happy的楠姐
    2020-12-18 15:16

    if you are using latest version of webdriver, infact anything above 2.20 then

    driver.switchTo().alert().accept();
    

    should work provided the alert is a javascript alert similar to the one we get when we click

    alert demo OR confirm pop-up demo

    Updated

    here this code will help you accept the alert

    driver = new FirefoxDriver();
    String baseUrl = "http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get(baseUrl);
    driver.switchTo().frame(0);
    driver.findElement(By.cssSelector("input[type=\"button\"]")).click();
    driver.switchTo().alert().accept();
    

提交回复
热议问题