Having trouble bypassing the alert in Google Chrome after automatically logging into Facebook using selenium-webdriver. I am using Javascript / JS language

const {Builder, By, Key} = require('selenium-webdriver');
const {Options} = require('selenium-webdriver/chrome');

const options = new Options().setAlertBehavior('dismiss');
let driver = new Builder().forBrowser('chrome').setChromeOptions(options).build();
driver.get('https://www.facebook.com/');

(async function demo() {
    (await driver).findElement(By.css('#email')).sendKeys('***');
    (await driver).findElement(By.css('#pass')).sendKeys('***', Key.RETURN);    
})();

/* Seeking a solution to prevent the "www.facebook.com wants to show you notifications" alert from appearing in Chrome. How can the Options() be modified to automatically accept or block this notification? Thank you for your assistance! */

Answer №1

Give this a try

//Set up a map to hold preferences 
Map<String, Object> preferences = new HashMap<String, Object>();

//Add key and value to the map to disable browser notifications
//Use 1 to allow and 2 to block
preferences.put("profile.default_content_setting_values.notifications", 2);

//Create a new ChromeOptions object 
ChromeOptions newOptions = new ChromeOptions();

// Set ExperimentalOption - preferences 
newOptions.setExperimentalOption("preferences", preferences);

//Pass the ChromeOptions object to initialize the Chrome driver to disable browser notifications
WebDriver newDriver = new ChromeDriver(newOptions);

or

Create an instance of the ChromeOptions class

ChromeOptions chromeOptions = new ChromeOptions();
Add the chrome switch to disable notifications - "--disable-notifications"

chromeOptions.addArguments("--disable-notifications");
Then specify the path for the driver executable

System.setProperty("webdriver.chrome.driver","path/to/driver/exe");
and then pass the ChromeOptions instance to the ChromeDriver Constructor

WebDriver newDriver = new ChromeDriver(chromeOptions);

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

Difficulty encountered in closing dialog box upon clicking NavLink in React

Currently, I am developing a React application and have integrated a dialog box using the Material-UI library's Dialog component. Inside this dialog box, there is a navigation menu that I would like to close when a user clicks on one of the navigation ...

Differences Between Vuetify Breakpoints and CSS Helper Classes

As I browse through the Vuetify documentation and various code snippets on the web, I often come across examples that mention using either a Vuetify breakpoint or a CSS helper class to make an element responsive to screen size changes. Is there a preferre ...

What is the best method for capturing a full display screenshot that includes the browser using webdriver?

Does anyone know how to capture a full screenshot of the display using WebDriver? The Selenium documentation mentions that it is possible: (http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/TakesScreenshot.html) .. The screenshot should ...

Selecting a "non-operational" collection location on an e-commerce platform

Recently I discovered a bug on an online shopping website. It seems that by using inspect element, it was possible to alter the HTML code and change an unavailable pickup point to available. This allowed me to place an order, make a payment, and even recei ...

Utilize the ng.IFilterService interface within a TypeScript project

I am facing an issue with a .ts file that contains the following code: module App.Filters { export class SplitRangeFilter implements ng.IFilterService { static $inject = ['$filter']; public static factory(): Function { ...

What are the benefits of removing event listeners in Reactjs?

In my opinion, the event listeners need to be reliable and consistent. React.useEffect(() => { const height = window.addEventListener("resize", () => { setWindowSize(window.innerHeight); }); return () => window.remov ...

Alter the background color of a div contingent on the checkbox being checked within

I am in search of a solution to modify the background color of a parent div that contains a checkbox. The condition is that the background color should only change when the 'checked' attribute is present within the tag. HTML <div class="comp ...

Tips for integrating the C++ icon into a ReactJs project

For a ReactJs project, I needed to include icons of different Languages and Tools. When adding a Python icon, I used: <ListItem> <ListItemIcon className={classes.icon}> <span className="iconify" data-icon= ...

Is there a way to switch an element across various pages within Ionic 3?

Is it possible to exchange information between two pages using logic? I have successfully implemented a checklist, but I am unsure how to add a success/error icon next to the Room name on the 'verifyplace.html' page after invoking the goToNextPa ...

Is there a way to access a computed property within methods?

Currently, I am utilizing this particular package to implement infinite scrolling in Vue. In order to continuously add new elements during each scroll, I fetch JSON data from my API server and store it in a data object. Subsequently, I divide the array in ...

Obtain GPS coordinates (latitude/longitude) from a Google map by dropping a marker on the

Is there a straightforward script that can load a Google Map and, when clicked, display a marker that saves the latitude and longitude values to a variable? Does anyone know if there is an existing PHP solution for this functionality? Appreciate any help ...

Retrieve WebDriver from Selenium jar for running tests

I have a selenium program that needs to be distributed to other computers, but I want to simplify the process by only providing a runnable jar file. When the program is executed, it should automatically extract chromedriver.exe to a new directory within th ...

What methods do certain API services use to accept Authorization headers from any source?

Payment processing platforms like Stripe and Square provide the capability to include your API key in an Authorization header as a Bearer token. However, my understanding is that allowing credentials, such as the Authorization header, from all origins is ...

The button is not visible, but it is returning TRUE. The button's xpath can be found on the webpage for the Selenium

When using Selenium WebDriver to check for the visibility of a button, the boolean value is consistently returning true even though the button is not visible on the web page. The code below is being used and returns TRUE. Despite seeing the xpath for the ...

Vue buttons causing table content to vanish

I've encountered an issue with my edit and delete functions where clicking the buttons in the table causes the data to disappear. Below is the full code snippet for reference: <h1>Player Rankings</h1> <!-- More HT ...

Is there a way to undo the changes made by the following execution using Javascript?

Similar Question: How can I delete a cookie using JavaScript? javascript:void(document.cookie=”PREF=ID=20b6e4c2f44943bb:U=4bf292d46faad806:TM=1249677602:LM=1257919388:S=odm0Ys-53ZueXfZG;path=/; domain=.google.com”); To undo the action perfor ...

What is the purpose of using Array.prototype.slice.call(nodeList) for handling DOM elements?

Many JavaScript libraries like jQuery and Zepto frequently use Array.prototype.slice.call on querySelectorAll(), getElementsByTag, or ClassName results. Despite browsing numerous questions and answers on StackOverflow about this topic, I understand that it ...

Establishing the bottom limit for the vertical axis on a column chart with the Visualization Google API

I am currently working on implementing the Google Chart API alongside AngularJS. I attempted to adjust the min property of the vertical axis for my column chart to 0 using the following code: var dataMois = CaDuMois.query(function(){ $scope.comparat ...

Managing multiple sets of data in a structured form similar to an array

How Do I Send Form Data as an Array? Take a look at the code snippet below. I'm having trouble setting the index in product_attribute['index must be here']['key'] <tr v-for="index in attributes"> <td class="text-left ...

What is the best way to ensure that one method waits for another method to complete before proceeding?

Below is a React method that needs completion for uploading images to cloudinary and setting the URL in state before executing the API call addStudent. The order of execution seems correct at first glance, but the last API call crashes due to base64 data n ...