0
 static WebDriver driver = new  FirefoxDriver();
    public static void main(String[] args) throws IOException, InterruptedException {
        // TODO code application logic here
        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter base url....!!!");
        String baseurl = br.readLine();
        connection c= new connection();
        c.makeConnection(driver,baseurl);
       JavascriptExecutor js;
      if (driver instanceof JavascriptExecutor) {
    js = (JavascriptExecutor)driver;
    js.executeScript("alert('hello world');");
  }

Following error occurs

Exception in thread "main" org.openqa.selenium.UnhandledAlertException: Modal dialog present: hello world
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15'
System info: host: 'comp74', ip: '192.168.0.74', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_17'
Session ID: f6ba3a2b-87d4-4192-ad1a-94cf260d8f6e
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=26.0}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
    at org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:174)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:141)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:332)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementsByXPath(RemoteWebDriver.java:408)
    at org.openqa.selenium.By$ByXPath.findElements(By.java:358)
    at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:295)
    at javaapplication4.count.linkCount(count.java:27)
    at javaapplication4.JavaApplication4.main(JavaApplication4.java:34)
3
  • Well what did you expect to happen? You are opening a JavaScript dialog that Selenium doesn't expect to be there. Commented Feb 4, 2014 at 12:51
  • i want a alert message in browser, while running this script. Commented Feb 4, 2014 at 12:54
  • Try getting handle of the current window and switch to this window after generating the alert. Commented Feb 4, 2014 at 13:03

1 Answer 1

1

Is per my understanding. After executing an alert if you try to do any other operation in the page when the alert already exists this errors comes. Are you providing the actual code?

Exception shows 
at javaapplication4.count.linkCount(count.java:27)
at javaapplication4.JavaApplication4.main(JavaApplication4.java:34)

as per above stacktrace you do something in linkCount. If you have anycode after .executeScript("alert('hello world' line. Please comment them and then check.

Sign up to request clarification or add additional context in comments.

4 Comments

yes you are right but i need this alert message, after clicking on ok button of this alert message rest script should run.
unless and until you close the alert message the script will not run. It will throw exception. to close the alert you can do Alert alert = driver.switchTo().alert(); alert.accept();
How should i close it in my script?
Alert alert = driver.switchTo().alert(); alert.accept();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.