简体   繁体   English

使用Watir时如何保存Chrome的设置?

[英]How to save Chrome's settings when using Watir?

Chrome is interrupting the flow and asking about whether I (the user) want to allow desktop notifications to be sent by the current site. Chrome浏览器正在中断流程,并询问我(用户)是否要允许当前站点发送桌面通知。 I've tried changing this in the Advanced Settings page of Chrome, but each time Watir instantiates a new browser instance all the settings are back to the default. 我曾尝试在Chrome的“高级设置”页面中更改此设置,但是每次Watir实例化新的浏览器实例时,所有设置都会恢复为默认设置。

Instead of dealing with code to handle this kind of thing, I'd much prefer to spawn an instance with settings I've specified but I can't find how to do this. 除了处理代码来处理这种事情外,我更喜欢使用指定的设置生成一个实例,但是我找不到执行该操作的方法。

Any help or insight with this is much appreciated. 任何帮助或有识之士对此深表感谢。

Watir Webdriver 0.9.1 Watir Webdriver 0.9.1

The above works but I have found that sometimes profile settings are not always picked up. 上面的作品,但我发现有时配置文件设置并非总是可以使用。 An alternative is to use chrome's command line switches. 一种替代方法是使用chrome的命令行开关。

You can add them like so: 您可以这样添加它们:

b = Watir::Browser.new :chrome, :switches => %w[--disable-notifications]

A full list of all command line switches can be found here. 可以在此处找到所有命令行开关的完整列表。 http://peter.sh/experiments/chromium-command-line-switches/ http://peter.sh/experiments/chromium-command-line-switches/

To note: Not all of these switches still work with the current version of chrome. 注意:并非所有这些开关都仍适用于当前版本的chrome。

when you run Watir test in temp directory created new empty profile with default settings 当您在temp目录中运行Watir测试时,使用默认设置创建了新的空配置文件

/tmp/.com.google.Chrome.couple_random_characters/Default /tmp/.com.google.Chrome.couple_random_characters/Default

web-driver for Firefox can use you profile: Firefox的网络驱动程序可以使用您的个人资料:

require 'watir-webdriver'

default_profile = Selenium::WebDriver::Firefox::Profile.from_name "default"
browser = Watir::Browser.new :firefox, :profile => default_profile

Chrome web-driver not have this functionality You can parse Chrome preference file for you config folder ( https://www.chromium.org/user-experience/user-data-directory ) Chrome网络驱动程序不具有此功能您可以为您的配置文件夹解析Chrome偏好设置文件( https://www.chromium.org/user-experience/user-data-directory

in Linux path to Preferences file: 在Linux中的Preferences文件路径:

/home/your_name/.config/google-chrome/Default/Preferences /home/your_name/.config/google-chrome/Default/Preferences

require 'watir-webdriver'

prefs_hash = {
#chrome preference what you need
#for example:
    'profile' => {
        'default_content_setting_values' => {'automatic_downloads' => 1}
    }
}
browser = Watir::Browser.new :chrome, :prefs => prefs_hash

(...chrome preference structure can be changed and not describe in documentation) (... chrome偏好设置结构可以更改,而在文档中未描述)

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

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