Watir 更改 Mozilla Firefox 首选项

发布于 2024-11-18 10:07:02 字数 481 浏览 1 评论 0原文

我正在使用 Watir 运行 Ruby 脚本来自动执行一些操作。我正在尝试自动将一些文件保存到某个目录。因此,在我的 Mozilla 设置中,我将默认下载目录设置为桌面并选择自动保存文件。

然而,当我开始运行脚本时,这些更改并未反映出来。似乎首选项恢复为默认值。我已包含以下内容

require "rubygems"         # Optional.
require "watir-webdriver"  # For web automation.
require "win32ole"         # For file save dialog.

并打开一个新的 Firefox 实例:

browser = Watir::Browser.new(:firefox)

关于为什么首选项会因此而推迟的任何想法?或者对于我想做的事情有什么替代想法吗? (自动保存文件)。

谢谢

I'm running a Ruby script using Watir to automate some things for me. I'm attempting to automatically save some files to a certain directory. So, in my Mozilla settings I set my default download directory to say the desktop and choose to automatically save files.

These changes, however, are not reflected when I begin to run my script. It seems like the preferences revert back to default. I've included the following

require "rubygems"         # Optional.
require "watir-webdriver"  # For web automation.
require "win32ole"         # For file save dialog.

and open a new firefox instance with:

browser = Watir::Browser.new(:firefox)

Any ideas on why the preferences would be set back by this? Or any alternative ideas for what I am trying to do? (Automatically save files).

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨光如昨 2024-11-25 10:07:02

WebDriver 为每个浏览器实例使用一个干净的配置文件,这就是首选项似乎被“重置”的原因。您可以告诉它使用默认配置文件:

Watir::Browser.new :firefox, :profile => "default" 

或在启动浏览器之前以编程方式调整配置文件首选项:

profile = Selenium::WebDriver::Firefox::Profile.new
profile['some.preference'] = true
profile.add_extension "/path/to/some/extension.xpi"

Watir::Browser.new :firefox, :profile => profile

有关配置自动文件下载的示例,请参阅 此部分

WebDriver uses a clean profile for each browser instance, which is why the preferences appear to be "reset". You can tell it to use your default profile:

Watir::Browser.new :firefox, :profile => "default" 

or tweak profile preferences programatically before launching the browser:

profile = Selenium::WebDriver::Firefox::Profile.new
profile['some.preference'] = true
profile.add_extension "/path/to/some/extension.xpi"

Watir::Browser.new :firefox, :profile => profile

For an example of configuring automatic file downloads, see this section on the Selenium wiki.

德意的啸 2024-11-25 10:07:02

的下载位置的默认 Watir 首选项

更改chrome

profile = Selenium::WebDriver::Chrome::Profile.new
download_dir = File.join(Rails.root, 'lib', 'assets')
profile['download.default_directory'] = download_dir
profile[download.prompt_for_download] = false
@b = Watir::Browser.new :chrome, :profile => profile

for firefox

profile = Selenium::WebDriver::Firefox::Profile.new    
download_dir = File.join(Rails.root, 'lib', 'assets')
profile['browser.download.dir'] = download_dir
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf"
@b = Watir::Browser.new. :firefox, :profile => profile

注意:为了能够从 Rails 应用程序中轻松访问 Rails.root/lib 文件夹,您需要将此代码或类似内容添加到您的配置/应用程序中.rb 文件:

config.autoload_paths += Dir["#{config.root}/lib/**/"]

了解更多信息:http://watirwebdriver.com/browser-downloads/

change default Watir preferences for download location

for chrome

profile = Selenium::WebDriver::Chrome::Profile.new
download_dir = File.join(Rails.root, 'lib', 'assets')
profile['download.default_directory'] = download_dir
profile[download.prompt_for_download] = false
@b = Watir::Browser.new :chrome, :profile => profile

for firefox

profile = Selenium::WebDriver::Firefox::Profile.new    
download_dir = File.join(Rails.root, 'lib', 'assets')
profile['browser.download.dir'] = download_dir
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf"
@b = Watir::Browser.new. :firefox, :profile => profile

note: to be be able to access the Rails.root/lib folder easily from within your rails app, you'll need to add this code or something like it to your config/application.rb file:

config.autoload_paths += Dir["#{config.root}/lib/**/"]

for more information: http://watirwebdriver.com/browser-downloads/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文