请按照以下步骤操作:
A) 使用 Java:
对于旧 Chrome 版本 (<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);
Run Code Online (Sandbox Code Playgroud)
对于新的 Chrome 版本 (>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);
Run Code Online (Sandbox Code Playgroud)
对于火狐:
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");
Run Code Online (Sandbox Code Playgroud)
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')
Run Code Online (Sandbox Code Playgroud)
C) 使用 C#:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-notifications"); // to disable notification
IWebDriver driver = new ChromeDriver(options);
Run Code Online (Sandbox Code Playgroud)
处理此类需求的步骤:
firefox.exe -p以启动配置文件管理器)customfirefox)about:permissions 现在使用新创建的配置文件通过调用 Firefox 来运行您的测试
ProfilesIni ffProfiles = new ProfilesIni();
FirefoxProfile profile = ffProfiles.getProfile("customfirefox");
WebDriver driver = new FirefoxDriver(profile);
Run Code Online (Sandbox Code Playgroud)
这将始终使用 Firefox 以及保存在配置文件中的自定义配置。
希望这对你有用..!!!
祝你好运。
| 归档时间: |
|
| 查看次数: |
20642 次 |
| 最近记录: |