Chrome Selenium禁用还原页面poup

2nc8po8w  于 12个月前  发布在  Go
关注(0)|答案(6)|浏览(122)

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

我试着设置配置文件的首选项,但它似乎它是不会改变的,代码:

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

我尝试将exit type的值更改为none & None,但在首选项文档中没有任何更改。

vsnjm48y

vsnjm48y1#

我使用的是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) 
    {
    }
}
kwvwclae

kwvwclae2#

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

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

它是由https://superuser.com/a/1343331所说的,唯一的问题是那里列出的方法,你每次都需要手动编辑文件,所以这更好,在2021年5月测试。只是不能投赞成票的答案,因为我还没有声誉,这是最后一次。

pgpifvop

pgpifvop3#

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

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

sdnqo3pr4#

我在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);
vsnjm48y

vsnjm48y5#

经过一个月的探索,我找到了一个解决方案。到处都写着的--disable-session-crashed-bubble参数是不相关的。使用--hide-crash-restore-bubble。用python + selenium测试,可以用。
范例:

...
    def _options(self):
        chrome_options = Options()
        chrome_options.add_argument('--hide-crash-restore-bubble')
        # some other options
        return chrome_options
...
dxpyg8gm

dxpyg8gm6#

试试这个代码:

prefs = {'exit_type': 'Normal'}

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

相关问题