简体   繁体   中英

Selenium Webdriver - No alert is present Error

public class Test1 {
    public static void main(String[] args) {
        String alertText = "";
        WebDriver driver = new FirefoxDriver();

        driver.manage().window().maximize(); 
        driver.get("https://gogiftz.com/default/");

        WebElement contact = driver.findElement(By.xpath(".//*[@id='root-wrapper']/div/div/div[3]/div/div[2]/div/div/div[1]/div/div/div[3]/div/div/ul/li[1]/a"));
        contact.click();

        driver.findElement(By.xpath(".//*[@id='telephone']")).sendKeys("04364249425");
        driver.findElement(By.xpath(".//*[@id='comment']")).sendKeys("aasdfghlkjhjghkj");
        driver.findElement(By.xpath(".//*[@id='name']")).sendKeys("abcdef");
        driver.findElement(By.xpath(".//*[@id='email']")).sendKeys("abcdef@gmail.com");
        driver.findElement(By.xpath(".//*[@id='contactForm']/div[2]/button")).submit();

        Alert alert = driver.switchTo().alert();
        alertText = alert.getText();
        System.out.println(alertText);
        alert.accept();
    }
}

I was trying to find alert present in the WebDriver using above code. But I am getting this type of error in below.

No alert is present (WARNING: The server did not provide any stacktrace information)

Help me to fix this Error...

below mentioned issue may be causing this exception

  1. Alert is not present on page, which i guess you already have checked. If not then revisit the code and perform the action that triggers the alert.

  2. Otherwise, It's because of synchronization issue. What it means is that as soon as page load is done the alert is not present on page and it might be appearing after a while so you can give it a try by putting wait statement.

    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));

You should wait for alert to be present before You interact with it.

It work better in older drivers, new require it.

So first wait for alert to be present, then interact with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM