【问题标题】:Selenium disable Restore pages poupSelenium 禁用恢复页面弹出窗口
【发布时间】:2018-12-18 13:47:57
【问题描述】:

我正在使用 selenium C#,我正在尝试禁用“崩溃”chrome 的弹出窗口:

我尝试设置配置文件首选项,但它似乎根本没有改变,代码:

        ChromeOptions options = new ChromeOptions();
        options.AddUserProfilePreference("exit_type", "Normal");
        options.AddUserProfilePreference("exited_cleanly", "true");
        IWebDriver driver = new ChromeDriver(options);

我尝试将退出类型的值更改为无和无,但在首选项文档中没有任何更改。

【问题讨论】:

    标签: c# google-chrome selenium selenium-chromedriver


    【解决方案1】:

    我正在使用 C#,并且我注意到只有在 finally 块中使用 Close() 方法后跟 Quit() 时才能正确关闭 chrome 驱动程序。不需要特殊选项。我认为在java中它是相同的方式。这将有助于在使用驱动程序启动 chrome 时摆脱“恢复页面”

    ChromeOptions options = new ChromeOptions();
    options.AddArgument(Configure.chromeProfileDir);
    options.AddArgument(Configure.chromePath);
    
    ChromeDriver d = null;
    
    try
    {
        d = new ChromeDriver(options);
        d.Navigate().GoToUrl("https://google.com");
    
        // Your operations...
    }
    catch(Exception e)
    {
        // Handle your exceptions...
    }
    finally 
    {
        try 
        {
            d.Close();
            d.Quit();
        } 
        catch(Exception e) 
        {
        }
    }
    

    【讨论】:

      【解决方案2】:

      我测试了@Icy 给出的答案,它对我有用。我用的是:

      prefs = {'exit_type': 'Normal'}
      option.add_experimental_option("prefs", {'profile': prefs})
      

      https://superuser.com/a/1343331 提到了它,唯一的问题是那里列出的方法,您每次都需要手动编辑文件,所以效果更好,在 2021 年 5 月进行了测试。只是无法投票回答,因为我还没有声誉,这是最后一个。

      【讨论】:

        【解决方案3】:

        使用下面的代码来处理这个弹出:

        ChromeOptions options = new ChromeOptions();
        options.AddArguments("--disable-extensions");
        options.AddArguments("--disable-application-cache");
        driver = new ChromeDriver(options);
        

        【讨论】:

        • 如果我不想禁用整个浏览器缓存?
        • 只需删除options.AddArguments("--disable-application-cache");。基本上对于你的问题,options.AddArguments("--disable-extensions");这个选项应该足够了。
        • 不幸的是,这并没有解决我的问题,而且我尝试使用这两个命令。
        • 你可以试试这个开关“--disable-infobars”以及“--disable-session-crashed-bubble”
        • 仍然没有任何变化
        【解决方案4】:

        我在java中试过这段代码,它解决了我的问题:))

            ChromeOptions options = new ChromeOptions();
            options.addArguments("user-data-dir="+profilepath);     
            options.addArguments("--no-startup-window");
            // argument "--no-startup-window" make chrome is failed to start -> selenium will quit chrome normaly 
            //-> start chrome again, it won't show restore page
            try {
                driver = new ChromeDriver(options); 
            }catch(Exception ex){                           
            }
            options = new ChromeOptions();
            options.addArguments("user-data-dir="+profilepath);
            driver = new ChromeDriver(options);
        

        【讨论】:

          【解决方案5】:

          试试这个代码:

          prefs = {'exit_type': 'Normal'}
          
          chrome_options.add_experimental_option("prefs", {'profile': prefs})
          

          【讨论】:

          • 一些评论解释了为什么这有助于回答 OP 的问题,将帮助人们在未来寻求类似问题的解决方案。
          猜你喜欢
          • 1970-01-01
          • 2021-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多