Unable to shut down a window with the command driver.close()

I am facing an issue where I am unable to close a window using the driver.close() method. Each time I try to execute it, an alert pops up.

One solution I attempted was:

JavascriptExecutor js = (JavascriptExecutor) driver;

String script = "window.onbeforeunload = null;";

js.executeScript(script);

driver.close();

However, even accepting the alert with

driver.switchTo().alert().accept()
is not working for me.

Another approach I tried involves:

driver.close();

System.out.println("After closing the window");//However, this line does not get executed.

It seems that the control simply halts at driver.close(), without throwing any exceptions or allowing the subsequent lines to be executed.

Answer №1

After some testing, I found a solution that successfully closed the window.

protected void closeWindowUsingJS(WebDriver driver) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    String script = "window.onbeforeunload = null;" + "window.close();";
    js.executeScript(script);
}

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

Mastering the stable usage of $watchIncorporating reliable

Please review the following code snippet: front_controller $scope.month=monthNames[$scope.currentmonthnumber]; monthyeartransfer.setvalue($scope.month); $scope.monthchange = function(m) { $scope.month = m; monthyeartransfer.setvalue($scope.month); ...

What methods can I use to differentiate between elements in an array?

Apologies if my title isn't clear, I'm struggling to figure out the next steps here. I've been teaching myself JavaScript. Right now, I'm working on creating a tic-tac-toe game. I'm using only vanilla Javascript and lodash for t ...

Middleware functions in Mongoose for pre and post actions are not being triggered when attempting to save

After carefully reviewing the documentation, I am still unable to pinpoint the issue. The pre & post middleware functions do not appear to be functioning as expected. I have made sure to update both my node version and all modules. // schema.js const sch ...

The technique for converting a JSON format date string into a date format

Currently, I am fetching data from a database using AJAX and displaying it in HTML text boxes for updating purposes. Below is the Web Method code that successfully retrieves the data: [WebMethod] public static List<Employee> getEmployee() { var ...

JavaScript - Modify input character prior to appending it to the text area

I am working on creating a virtual keyboard using jQuery. Whenever I press 'a' in the textarea, I want it to display 'z' instead. In my investigation of typing a letter in the textarea, I discovered the following sequence: keyDown ev ...

tips for replacing multiple route parameters in Angular using the replace function

I am facing an issue when trying to replace multiple parameters using the angular replace function. The problem is that the function only detects the first parameter. For example, if I have this route admin/management/{type}/card/{id}, and use the route.r ...

Creating a JavaScript array filled with database data using PHP

Below is a snippet of PHP code used to establish a connection to a database: <?php $host = "localhost"; $user = "admin"; $password = ""; $dbname = "test"; $con = new mysqli($host, $user, $password, $dbname) or die ('Could not connect to the d ...

There was a failure to establish a Redis connection to the server with the address 127.0.0.1 on port 6379

Currently, I am working with node.js using expressjs. My goal is to store an account in the session. To test this out, I decided to experiment with sessions by following the code provided on expressjs var RedisStore = require('connect-redis')(ex ...

Efficient security testing accomplished through automating with Selenium

Exploring various strategies for conducting web application security testing utilizing Selenium WebDriver is my current focus. While I am familiar with XSS and SQL injection, I have yet to experiment with executing these tests using Selenium. Despite its p ...

How can I create and utilize a nested array within an ngFor loop?

*ngFor="let arr = ['foo', 'bar', 'sla']; let item of arr; let i = index;" Why does setting the instantiation of arr before let item of arr; result in an exception displaying [object Object]? Why am I unable to structure it th ...

Tips for arranging a Hashmap in JSP

A while ago, I created a Hashmap in JSP and now I need to figure out how to sort it. Is there a way to achieve this in JSP? <jsp:useBean id="myHashMAp" class="java.util.HashMap" scope="request"/> <c:set target="${myHashMAp}" property="${somePro ...

How can I effectively utilize the Selenium web driver to interact with buttons on a webpage?

<button style="margin: 8px 5px 0 0; height: 40px;" class="gprospect_linkedin_button gprospect_linkedin_button_style_medium"> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5 ...

Run Chromedriver in a Linux environment using Java

Exploring Eclipse, Selenium, and chromedriver to automate website data retrieval in headless mode has been an interesting project. Everything works smoothly on my laptop, but when I deployed a WAR version to a Java/Linux hosting service, I encountered an e ...

Is there a way to programmatically prevent the back button from functioning if the previous route pathname in React was 'Login'?

When it comes to navigating back on previous pages, the traditional back button is typically used instead of relying solely on the navigation bar. However, I am currently looking to disable this feature specifically when the next previous route in line is ...

Express's error handling mechanism fails to handle errors in asynchronous functions

I have been experimenting with implementing Error Handling in Express on async functions using try and catch, as well as creating a separate file. However, I am facing an issue where when an error occurs, Express does not handle it properly and the site cr ...

Conceal div elements and retain their status when the page is reloaded or switched

I currently have 3 div elements displayed on a webpage: header-div fixed_menu_div page_cont Each of these divs are styled with the following CSS properties: #header-div { top:0; left:0; display:inline; float:left; } #page_cont { mar ...

Safari automation test encounters hanging issues with driver execution

Safari Version: 11.0.2 MAC Version: 10.12.6 Mac OS Sierra selenium-java Version: 3.5.3 Language: JAVA For some reason, when running a selenium test using the Safari Driver, it seems to hang after completing a few steps. This issue doesn't seem to fol ...

Creating a URL using input fields with JavaScript or jQuery

I'm currently working on developing a form that acts as a URL builder using JavaScript or jQuery. The main concept behind this form is to take the user-input values from two fields, combine them with a preset URL structure, and display the resulting ...

Leverage the power of axios in your React application to retrieve information from an

I have been exploring how to use axios for fetching data from an API (https://reqres.in/) and displaying it in my React application. Previously, I used the fetch method in JavaScript to retrieve data from APIs. Despite trying various resources, I am unsure ...

yii CGridView refresh trigger an influx of Ajax requests

Does anyone know why the yii cgridview refresh button triggers multiple ajax calls? Upon refreshing, I have noticed that it initiates several ajax calls (this time it's 3, but sometimes it's 4 or even 5) GET http://localhost/ijob-css/index.php/ ...