gpt4 book ai didi

selenium - 如何使用 Selenium Webdriver 在“显​​示通知”弹出窗口上单击“允许”

转载 作者:行者123 更新时间:2023-12-02 12:10:51 25 4
gpt4 key购买 nike

我正在尝试登录 Facebook。成功登录后,我会看到一个浏览器弹出窗口:

Show Notifications

如何使用网络驱动程序单击“允许”并继续?

最佳答案

请按照以下步骤操作:

A) 使用 JAVA :

For Old Chrome Version (<50):

//Create a instance of ChromeOptions class
ChromeOptions options = new ChromeOptions();

//Add chrome switch to disable notification - "**--disable-notifications**"
options.addArguments("--disable-notifications");

//Set path for driver exe
System.setProperty("webdriver.chrome.driver","path/to/driver/exe");

//Pass ChromeOptions instance to ChromeDriver Constructor
WebDriver driver =new ChromeDriver(options);
<小时/>

For New Chrome Version (>50):

//Create a map to store  preferences 
Map<String, Object> prefs = new HashMap<String, Object>();

//add key and value to map as follow to switch off browser notification
//Pass the argument 1 to allow and 2 to block
prefs.put("profile.default_content_setting_values.notifications", 2);

//Create an instance of ChromeOptions
ChromeOptions options = new ChromeOptions();

// set ExperimentalOption - prefs
options.setExperimentalOption("prefs", prefs);

//Now Pass ChromeOptions instance to ChromeDriver Constructor to initialize chrome driver which will switch off this browser notification on the chrome browser
WebDriver driver = new ChromeDriver(options);
<小时/>

For Firefox :

WebDriver driver ;
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("permissions.default.desktop-notification", 1);
DesiredCapabilities capabilities=DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
driver = new FirefoxDriver(capabilities);
driver.get("http://google.com");
<小时/>

B) 使用Python:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

option = Options()

option.add_argument("--disable-infobars")
option.add_argument("start-maximized")
option.add_argument("--disable-extensions")

# Pass the argument 1 to allow and 2 to block
option.add_experimental_option(
"prefs", {"profile.default_content_setting_values.notifications": 1}
)

driver = webdriver.Chrome(
chrome_options=option, executable_path="path-of-driver\chromedriver.exe"
)
driver.get("https://www.facebook.com")

C) 使用 C#:

ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-notifications"); // to disable notification
IWebDriver driver = new ChromeDriver(options);

关于selenium - 如何使用 Selenium Webdriver 在“显​​示通知”弹出窗口上单击“允许”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38684175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com