简体   繁体   English

如何关闭Chrome浏览器弹出窗口?

[英]How to close a chrome browser pop up window?

I am writing a code for Facebook where it takes the URL, ID, Password from a properties file but upon logging in I am hinted with a "Facebook wants to show notifications - Allow - Block" How do I make it so after login it (A.) Presses ESP or ALT+F4 and closes the popup or (B.) Finds the notification and closes it itself. 我正在为Facebook编写一个代码,它从属性文件中获取URL,ID,密码,但登录后我暗示“Facebook想要显示通知 - 允许 - 阻止”如何在登录后如此制作( A.)按下ESP或ALT + F4并关闭弹出窗口或(B.)查找通知并自行关闭。 This is what Im using but its not working. 这就是我使用但它不起作用。 Any help is appreciated. 任何帮助表示赞赏。

    public void closePopup() throws InterruptedException{

    Thread.sleep(1000);
    Actions action=new Actions(driver);

    action.keyDown(Keys.ESCAPE).keyUp(Keys.ESCAPE).build().perform();

After further research I found my answer. 经过进一步研究,我找到了答案。 It is a chrome notification so here is the required step to solve my problem. 这是一个chrome通知,所以这是解决我的问题所需的步骤。

        ChromeOptions ops = new ChromeOptions();
        ops.addArguments("--disable-notifications");
        System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
        driver = new ChromeDriver(ops);

Please Follow below steps : 请按照以下步骤操作:

Step 1: 步骤1:

//Create a instance of ChromeOptions class //创建ChromeOptions类的实例

ChromeOptions options = new ChromeOptions();

Step 2: 第2步:

//Add chrome switch to disable notification - " --disable-notifications " //添加chrome开关以禁用通知 - “ - disable -notifications

options.addArguments("--disable-notifications");

Step 3: 第3步:

//Set path for driver exe //设置驱动程序exe的路径

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

Step 4 : 第四步 :

//Pass ChromeOptions instance to ChromeDriver Constructor //将ChromeOptions实例传递给ChromeDriver构造函数

WebDriver driver =new ChromeDriver(options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM