How to use prefs method of Selenium.WebDriver.Chrome Package

Best Selenium code snippet using Selenium.WebDriver.Chrome.prefs

options.rb

Source:options.rb Github

copy

Full Screen

...17module Selenium18 module WebDriver19 module Chrome20 class Options21 attr_reader :args, :prefs, :options, :emulation, :extensions, :encoded_extensions22 attr_accessor :binary23 KEY = 'goog:chromeOptions'.freeze24 #25 # Create a new Options instance.26 #27 # @example28 # options = Selenium::WebDriver::Chrome::Options.new(args: ['start-maximized', 'user-data-dir=/tmp/temp_profile'])29 # driver = Selenium::WebDriver.for(:chrome, options: options)30 #31 # @param [Hash] opts the pre-defined options to create the Chrome::Options with32 # @option opts [Array<String>] :args List of command-line arguments to use when starting Chrome33 # @option opts [String] :binary Path to the Chrome executable to use34 # @option opts [Hash] :prefs A hash with each entry consisting of the name of the preference and its value35 # @option opts [Array<String>] :extensions A list of paths to (.crx) Chrome extensions to install on startup36 # @option opts [Hash] :options A hash for raw options37 # @option opts [Hash] :emulation A hash for raw emulation options38 #39 def initialize(**opts)40 @args = Set.new(opts.delete(:args) || [])41 @binary = opts.delete(:binary) || Chrome.path42 @prefs = opts.delete(:prefs) || {}43 @extensions = opts.delete(:extensions) || []44 @options = opts.delete(:options) || {}45 @emulation = opts.delete(:emulation) || {}46 @encoded_extensions = []47 end48 #49 # Add an extension by local path.50 #51 # @example52 # options = Selenium::WebDriver::Chrome::Options.new53 # options.add_extension('/path/to/extension.crx')54 #55 # @param [String] path The local path to the .crx file56 #57 def add_extension(path)58 raise Error::WebDriverError, "could not find extension at #{path.inspect}" unless File.file?(path)59 raise Error::WebDriverError, "file was not an extension #{path.inspect}" unless File.extname(path) == '.crx'60 @extensions << path61 end62 #63 # Add an extension by Base64-encoded string.64 #65 # @example66 # options = Selenium::WebDriver::Chrome::Options.new67 # options.add_encoded_extension(encoded_string)68 #69 # @param [String] encoded The Base64-encoded string of the .crx file70 #71 def add_encoded_extension(encoded)72 @encoded_extensions << encoded73 end74 #75 # Add a command-line argument to use when starting Chrome.76 #77 # @example Start Chrome maximized78 # options = Selenium::WebDriver::Chrome::Options.new79 # options.add_argument('start-maximized')80 #81 # @param [String] arg The command-line argument to add82 #83 def add_argument(arg)84 @args << arg85 end86 #87 # Add a new option not yet handled by bindings.88 #89 # @example Leave Chrome open when chromedriver is killed90 # options = Selenium::WebDriver::Chrome::Options.new91 # options.add_option(:detach, true)92 #93 # @param [String, Symbol] name Name of the option94 # @param [Boolean, String, Integer] value Value of the option95 #96 def add_option(name, value)97 @options[name] = value98 end99 #100 # Add a preference that is only applied to the user profile in use.101 #102 # @example Set the default homepage103 # options = Selenium::WebDriver::Chrome::Options.new104 # options.add_preference('homepage', 'http://www.seleniumhq.com/')105 #106 # @param [String] name Key of the preference107 # @param [Boolean, String, Integer] value Value of the preference108 #109 def add_preference(name, value)110 prefs[name] = value111 end112 #113 # Run Chrome in headless mode.114 #115 # @example Enable headless mode116 # options = Selenium::WebDriver::Chrome::Options.new117 # options.headless!118 #119 def headless!120 add_argument '--headless'121 # https://bugs.chromium.org/p/chromium/issues/detail?id=737678#c1122 add_argument '--disable-gpu' if WebDriver::Platform.windows?123 end124 #125 # Add an emulation device name126 #127 # @example Start Chrome in mobile emulation mode by device name128 # options = Selenium::WebDriver::Chrome::Options.new129 # options.add_emulation(device_name: 'iPhone 6')130 #131 # @example Start Chrome in mobile emulation mode by device metrics132 # options = Selenium::WebDriver::Chrome::Options.new133 # options.add_emulation(device_metrics: {width: 400, height: 800, pixelRatio: 1, touch: true})134 #135 # @param [String] device_name Name of the device or a hash containing width, height, pixelRatio, touch136 # @param [Hash] device_metrics Hash containing width, height, pixelRatio, touch137 # @param [String] user_agent Full user agent138 #139 def add_emulation(device_name: nil, device_metrics: nil, user_agent: nil)140 @emulation[:deviceName] = device_name if device_name141 @emulation[:deviceMetrics] = device_metrics if device_metrics142 @emulation[:userAgent] = user_agent if user_agent143 end144 #145 # @api private146 #147 def as_json(*)148 extensions = @extensions.map do |crx_path|149 File.open(crx_path, 'rb') { |crx_file| Base64.strict_encode64 crx_file.read }150 end151 extensions.concat(@encoded_extensions)152 opts = @options153 opts[:binary] = @binary if @binary154 opts[:args] = @args.to_a if @args.any?155 opts[:extensions] = extensions if extensions.any?156 opts[:mobileEmulation] = @emulation unless @emulation.empty?157 opts[:prefs] = @prefs unless @prefs.empty?158 {KEY => opts}159 end160 end # Options161 end # Chrome162 end # WebDriver163end # Selenium...

Full Screen

Full Screen

driver.rb

Source:driver.rb Github

copy

Full Screen

...78 WebDriver.logger.deprecate 'Selenium::WebDriver::Chrome::Driver#new with `:detach` parameter',79 'Selenium::WebDriver::Chrome::Options#new or Options#add_option'80 options.add_option(:detach, opts.delete(:detach))81 end82 prefs = opts.delete(:prefs)83 if prefs84 WebDriver.logger.deprecate ':prefs', 'Selenium::WebDriver::Chrome::Options#add_preference'85 prefs.each do |key, value|86 options.add_preference(key, value)87 end88 end89 options = options.as_json90 caps.merge!(options) unless options[Options::KEY].empty?91 if opts.key?(:proxy) || opts.key?('proxy')92 WebDriver.logger.deprecate 'Selenium::WebDriver::Chrome::Driver#new with `:proxy` parameter',93 'Selenium::WebDriver::Chrome::Capabilities#proxy='94 caps[:proxy] = opts.delete(:proxy) if opts.key?(:proxy)95 caps[:proxy] ||= opts.delete('proxy') if opts.key?('proxy')96 end97 caps98 end99 end # Driver...

Full Screen

Full Screen

prefs

Using AI Code Generation

copy

Full Screen

1driver = Selenium::WebDriver.for :chrome, prefs: { download: { prompt_for_download: false, default_directory: '/tmp' } }2options.add_preference(:download, prompt_for_download: false, default_directory: '/tmp')3driver = Selenium::WebDriver.for :chrome, prefs: { download: { prompt_for_download: true, default_directory: '/tmp' } }4driver = Selenium::WebDriver.for :chrome, prefs: { download: { prompt_for_download: true, default_directory: '/tmp' } }

Full Screen

Full Screen

prefs

Using AI Code Generation

copy

Full Screen

1options.add_argument('--user-data-dir=/Users/username/Library/Application Support/Google/Chrome/Default')2options.add_argument('--profile-directory=Profile 1')3options.add_argument('--start-maximized')

Full Screen

Full Screen

prefs

Using AI Code Generation

copy

Full Screen

1prefs = {download: {prompt_for_download: false, default_directory: 'C:/Users/username/Downloads'}}2prefs = {download: {prompt_for_download: false, default_directory: 'C:/Users/username/Downloads'}}3options.add_preference(:download, prompt_for_download: false, default_directory: 'C:/Users/username/Downloads')

Full Screen

Full Screen

prefs

Using AI Code Generation

copy

Full Screen

1prefs = {2 download: {3 }4}5options.add_preference(:download, prefs)6options.add_chrome_option(:prefs, {download: {prompt_for_download: false, default_directory: '/home/username/Downloads'}})7options.add_argument("--disable-extensions")8options.add_argument("--disable-popup-blocking")9options.add_argument("--disable-notifications")10options.add_argument("--disable-infobars")11options.add_argument("--start-maximized")12options.add_option(:prefs, {download: {prompt_for_download: false, default_directory: '/home/username/Downloads'}})

Full Screen

Full Screen

prefs

Using AI Code Generation

copy

Full Screen

1prefs = {2 download: {3 }4}5options.add_preference(:download, prefs)6options.add_chrome_option(:prefs, {download: {prompt_for_download: false, default_directory: '/home/username/Downloads'}})7options.add_argument("--disable-extensions")8options.add_argument("--disable-popup-blocking")9options.add_argument("--disable-notifications")10options.add_argument("--disable-infobars")11options.add_argument("--start-maximized")12options.add_option(:prefs, {download: {prompt_for_download: false, default_directory: '/home/username/Downloads'}})

Full Screen

Full Screen

prefs

Using AI Code Generation

copy

Full Screen

1prefs.add_preference(:download, {default_directory: "C:/Users/Downloads"})2driver.find_element(:link_text, "some-file.txt").click3system("explorer.exe C:/Users/Downloads")4system("taskkill /F /IM explorer.exe")5system("taskkill /F /IM cmd.exe")6prefs.add_preference(:download, {default_directory: "C:/Users/Downloads"})7driver.find_element(:link_text, "some-file.txt").click8system("explorer.exe C:/Users/Downloads")9system("taskkill /F /IM explorer.exe")10system("taskkill /F /IM cmd.exe")

Full Screen

Full Screen

prefs

Using AI Code Generation

copy

Full Screen

1prefs = {2 'profile.default_content_setting_values' => {3 }4}5options.add_argument('--disable-notifications')6options.add_argument('--disable-infobars')

Full Screen

Full Screen

prefs

Using AI Code Generation

copy

Full Screen

1driver = Selenium::WebDriver.for :chrome, prefs: { download: { default_directory: download_directory } }2driver.find_element(:css, 'a[href="some-file.txt"]').click3file_path = File.join(download_directory, 'some-file.txt')4raise 'File not downloaded' unless File.exist?(file_path)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful