An error occurred while running JavaScript in Selenium WebDriver

 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');");
  }

An error message is displayed as follows:

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)

Answer №1

Based on my understanding, when an alert is executed and any other operation is attempted on the page where the alert already exists, this error occurs. Have you provided the actual code?

The exception is thrown at javaapplication4.count.linkCount(count.java:27)
at javaapplication4.JavaApplication4.main(JavaApplication4.java:34)

According to the above stack trace, something is being done in linkCount. If there is any code after

.executeScript("alert('hello world'
line, please comment them out and then check.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

LoadJSON data in P5 JavaScript is delayed

When attempting to read a Json file using loadJSON from p5, I am encountering a delay in receiving the data. When I use console.log(JSON.stringify(data)), it displays {}. function setup() { for(i = 0; i <= 3; i++) { //Loading the ...

What purpose does including an additional object serve after the error function callback when making a promise call?

Looking at this library https://github.com/arunisrael/angularjs-geolocation/blob/master/src/geolocation.js#L39 I searched extensively on the internet and even checked the official Mozilla documentation regarding promises, but found no results. I am confus ...

What should the initial argument be for the Helium driver.execute_script method?

I came across the following HTML snippet, which represents a dropdown menu. <select class="single-option-selector no-select selector single-option-selector-100" data-option="option1" id="product-select-template--15646112383191__ ...

Display refined outcomes on the search results page

In my app, the main feature is a search box on the homepage. Users can input their search queries and upon submission, they are redirected to a result page displaying the relevant results along with filtering options. The filtering functionality allows use ...

having difficulty locating the appropriate CSS selector to extract the title for web scraping

Hello everyone! I am attempting to retrieve the titles of Zomato restaurants, but I am facing an issue with my CSS selector not working correctly in the final block of the loop. I have also left a commented XPath for reference. Can someone please assist ...

The xpath property "text()" does not seem to be functioning as expected with a textnode in the code provided for Selenium

Snippet of HTML Code: <div class="content"> <div id="tab"> <div class="row"> <div class="col-sm-12"> <div> <div> <div> </div> <div> ...

Incorporating an HTML element into a Vue template

I'm currently utilizing a chart component (Chartist) that needs an HTML element to serve as a parent during SVG rendering. The elements that the chart requires are generated within a v-for loop, meaning they are not part of the DOM when the chart is ...

vue-awesome-swiper / The button's name is synchronized, causing all other swiper elements to move in unison

<template> <swiper v-for="item in items" :key="item.id" :options="swiperOption" ref="mySwiper" @someSwiperEvent="callback"> <swiper-slide>I'm Slide 1</swiper-slide> <swiper-slide>I'm Slide 2</swiper-slid ...

Integrating a Vue application with an OpenId provider using the OpenId Connect library

Currently, I am in the process of developing a Single Page Application with Vue on the client-side and Java Spring REST APIs on the backend. My goal is to add security measures using OpenId Connect, specifically with RapidIdentity as the provider. Unlike ...

Using regular expressions, divide the string at every occurrence of a period unless there is a quotation mark immediately following the period, in which case

Can anyone help me split this string? It includes quotation marks: "This is a test. I need this to be splitted." And here is one with a question? I am looking for: ['"This is a test.', 'I need this to be splitted."' ...

How can Javascript or Jquery determine the specific event assigned to an object?

Is it possible to retrieve the properties of HTML elements using their name or id? For example: document.getElementById('id').value; //Accessing element property in JavaScript $('#id').attr('class'); // Accessing correspond ...

A step-by-step guide to extracting a JSON object from an HTTP request using Java

Currently, I am attempting to retrieve a JSON Object using an HTTP request in Java code. I am seeking guidance on how to obtain the response or JSON object within the following code. Your help is much appreciated. (I am attempting to fetch Wikipedia cat ...

Maintaining Child Coordinates After Resizing Layout in Android Java

Issue: I am facing an issue with my Custom Layout. When I scale it using the given method, all child elements appear in the correct position visually. However, when I retrieve their coordinates, they remain in their original location or respond to touch ev ...

Executing a function by clicking on an element with the `ng-click` directive within a Bootstrap modal

I'm working on an app that allows users to submit posts for review. When a user clicks the add post button, I have a Bootstrap modal pop up to confirm their choice. Within this modal, there is a "confirm" button that should trigger a function. Strang ...

Guide on clicking a link and launching it in a new tab using geckodriver with selenium

I am attempting to click on a specific web element cells[-1] so that it opens in a new tab. From there, I plan to download something from a link on that page before closing the tab and returning to the original one. My current approach involves using the ...

Guide to creating a React Hooks counter that relies on the functionality of both a start and stop button

I am looking to create a counter that starts incrementing when the start button is clicked and stops when the stop button is pressed. Additionally, I want the counter to reset to 1 when it reaches a certain value, for example 10. I have tried using setInte ...

Preserve the status of a CheckBox in Android Studio

As a novice in Java programming, I am facing an issue with saving checkbox statuses. I need to create a list where the options are saved locally within the app itself. The problem arises when trying to save the status of multiple checkboxes. The code curr ...

Encountering a syntax error when attempting to generate a div dynamically with jQuery

I am in the process of creating a view application form using Bootstrap after submitting a form. Initially, I created it utilizing two 'div' elements. Now, I am exploring how to dynamically generate a div upon clicking a button: <i> Sectio ...

I'm looking to locate the API documentation for AngularJS TypeScript

After transitioning from using AngularJS 1.4 and plain JavaScript to now working with AngularJS 1.5 but utilizing TypeScript, I have found it challenging to find helpful documentation. For instance, when trying to inject services like $q or $timeout into m ...

Cover a cube with a material using Three.js

After following the tutorial on creating a Cube with a texture, I encountered an issue where the texture repeated on every face of the cube. I am seeking a solution to use a single texture that 'wraps' around the cube. Is there a way to achieve t ...