Java 如何使用 Selenium Webdriver 处理浏览器级别的通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38367762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle browser level notification using Selenium Webdriver
提问by user2085714
I am Automating some test cases using Selenium Webdriver and core Java,in chrome browser for one test case on clicking button I am getting browser level notification 'Show notifications with options Allow and Block'. I want to select Allow option. Can anyone know how to handle this kind of notifications using Selenium webdriver.
please refer following snapshot for more details
我正在使用 Selenium Webdriver 和核心 Java 自动化一些测试用例,在 chrome 浏览器中单击按钮的一个测试用例我收到浏览器级别的通知“显示带有允许和阻止选项的通知”。我想选择允许选项。谁能知道如何使用 Selenium webdriver 处理此类通知。请参考以下快照了解更多详情
回答by Praveen
Steps to handle this type of requirements:
处理此类需求的步骤:
- Open Firefox Profile manager (Go to Start Menu, Type
firefox.exe -p
to launch profile manager) - Create a new Profile (Ex. name of Profile is
customfirefox
) - Navigate to
about:permissions
- Do the required configurations and Save the Profile
- 打开 Firefox 配置文件管理器(转到开始菜单,键入
firefox.exe -p
以启动配置文件管理器) - 创建一个新的配置文件(例如配置文件的名称是
customfirefox
) - 导航
about:permissions
- 进行所需的配置并保存配置文件
Now use the newly created Profile to run your test by invoking the Firefox
现在使用新创建的配置文件通过调用 Firefox 来运行您的测试
ProfilesIni ffProfiles = new ProfilesIni();
FirefoxProfile profile = ffProfiles.getProfile("customfirefox");
WebDriver driver = new FirefoxDriver(profile);
This will always use the firefox with the custom configurations saved on the profile.
这将始终使用带有保存在配置文件中的自定义配置的 Firefox。
Hope this works for you..!!!
希望这对你有用..!!!
Good luck.
祝你好运。
回答by Chandra Shekhar
update: Please use incognito mode to avoid such browser-level notifications and cookies issue.
更新:请使用隐身模式以避免此类浏览器级别的通知和 cookie 问题。
回答by Deepak10
As per my understanding,this pop-ups not javascripts one.So selenium won't handle them. Same kinda,authentication popup also there , which we automate via a hack like http://username:[email protected], here giving user name and password in url itself. In your case the way Praveen mentioned you have to create browser profile (FF or chrome ).In chrome, profile can be create via desired cabilities options or chrome options function.
根据我的理解,这个弹出窗口不是 javascripts one。所以 selenium 不会处理它们。同样,身份验证弹出窗口也在那里,我们通过像http://username:[email protected]这样的 hack 自动化,这里在 url 本身中提供用户名和密码。在您的情况下,Praveen 提到您必须创建浏览器配置文件(FF 或 chrome )。在 chrome 中,可以通过所需的电缆选项或 chrome 选项功能创建配置文件。
回答by Pritam Maske
Please Follow below steps :
请按照以下步骤操作:
A) USING JAVA :
A) 使用 Java:
For Old Chrome Version (<50):
对于旧 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);
For New Chrome Version (>50):
对于新的 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);
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) USING PYTHON :
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) USING C#:
C) 使用 C#:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-notifications"); // to disable notification
IWebDriver driver = new ChromeDriver(options);
回答by pritesh patel
You can use below code to allow chrome to send notifications:
您可以使用以下代码来允许 chrome 发送通知:
ChromeOptions options=new ChromeOptions();
Map<String, Object> prefs=new HashMap<String,Object>();
prefs.put("profile.default_content_setting_values.notifications", 1);
//1-Allow, 2-Block, 0-default
options.setExperimentalOption("prefs",prefs);
ChromeDriver driver=new ChromeDriver(options);
It works perfectly for me. Let me know if it doesn't work for you. Cheers!!
它非常适合我。如果它对您不起作用,请告诉我。干杯!!
回答by Rock
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("your Web site");