如何配置Chrome Options屏蔽Selenium自动登录Facebook后的通知弹窗?
Got it, let's tackle that stubborn notification popup! The reason your alertBehavior: 'dismiss' setting isn't working is because this isn't a standard JavaScript alert—it's a browser permission prompt for notifications. Those are controlled by Chrome's content settings, not the alert behavior config.
Here are two solid ways to block or auto-handle these prompts:
1. Use Chrome Preferences to Block Notifications
You can directly set Chrome's default notification permission to "Block" using the prefs option in ChromeOptions. This will automatically deny any site's request to send notifications:
const {Builder, By, Key} = require('selenium-webdriver'); const {Options} = require('selenium-webdriver/chrome'); // Configure Chrome options to block notifications const options = new Options() .setUserPreferences({ 'profile.default_content_setting_values.notifications': 2 // 2 = Block, 1 = Allow, 0 = Default }); let driver = new Builder() .forBrowser('chrome') .setChromeOptions(options) .build(); (async function demo() { await driver.get('https://www.facebook.com/'); await driver.findElement(By.css('#email')).sendKeys('***'); await driver.findElement(By.css('#pass')).sendKeys('***', Key.RETURN); })();
2. Alternative: Use Experimental Chrome Options
If the above method doesn't work for your Chrome version, you can use a quick command-line flag to disable all notifications entirely in the browser session:
const {Builder, By, Key} = require('selenium-webdriver'); const {Options} = require('selenium-webdriver/chrome'); const options = new Options() .addArguments('--disable-notifications') // Disable all notification requests .addArguments('--disable-popup-blocking'); // Optional: Block other unwanted popups too let driver = new Builder() .forBrowser('chrome') .setChromeOptions(options) .build(); (async function demo() { await driver.get('https://www.facebook.com/'); await driver.findElement(By.css('#email')).sendKeys('***'); await driver.findElement(By.css('#pass')).sendKeys('***', Key.RETURN); })();
Key Notes:
- For the preferences method, the value
2tells Chrome to block all notification requests. If you ever wanted to auto-accept instead, you'd use1;0keeps Chrome's default "ask user" behavior. - The
--disable-notificationsflag is a straightforward one-liner that turns off all notifications for the temporary Chrome session Selenium creates, which is perfect for avoiding this specific popup.
Either of these methods should stop that Facebook notification prompt from appearing. Just pick the one that works best with your Chrome and Selenium version!
内容的提问来源于stack exchange,提问作者Michael

