123456789_123456789_123456789_123456789_123456789_

Class: Selenium::WebDriver::Driver

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: rb/lib/selenium/webdriver/common/driver.rb

Overview

The main class through which you control the browser.

Constant Summary

SearchContext - Included

FINDERS

Class Method Summary

Instance Attribute Summary

Instance Method Summary

TakesScreenshot - Included

#save_screenshot

Save a PNG screenshot of the viewport to the given path.

#screenshot_as

Return a PNG screenshot in the given format as a string.

SearchContext - Included

#find_element

Find the first element matching the given arguments.

#find_elements

Find all elements matching the given arguments.

#extract_args

Constructor Details

.new(bridge: nil, listener: nil) ⇒ Driver

A new Driver instance with the given bridge. End users should use Selenium::WebDriver.for instead of using this directly.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 71

def initialize(bridge: nil, listener: nil, **)
  @devtools = nil
  bridge ||= create_bridge(**)
  @bridge = listener ? Support::EventFiringBridge.new(bridge, listener) : bridge
  add_extensions(@bridge.browser)
end

Class Method Details

.for(browser, opts = {}) ⇒ Driver

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 44

def for(browser, opts = {})
  case browser
  when :chrome, :chrome_headless_shell
    Chrome::Driver.new(**opts)
  when :internet_explorer, :ie
    IE::Driver.new(**opts)
  when :safari
    Safari::Driver.new(**opts)
  when :firefox, :ff
    Firefox::Driver.new(**opts)
  when :edge, :microsoftedge, :msedge
    Edge::Driver.new(**opts)
  when :remote
    Remote::Driver.new(**opts)
  else
    raise ArgumentError, "unknown driver: #{browser.inspect}"
  end
end

Instance Attribute Details

#bridge (readonly, private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 355

attr_reader :bridge

Instance Method Details

#[](sel) ⇒ WebDriver::Element

Get the first element matching the given selector. If given a String or Symbol, it will be used as the id of the element.

Examples:

driver['someElementId']    #=> #<WebDriver::Element:0x1011c3b88>
driver[:tag_name => 'div'] #=> #<WebDriver::Element:0x1011c3b88>

Parameters:

  • sel (String, Hash) —

    id or selector

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 330

def [](sel)
  sel = {id: sel} if sel.is_a?(String) || sel.is_a?(Symbol)

  find_element sel
end

#action ⇒ ActionBuilder

See Also:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 145

def action(**)
  bridge.action(**)
end

#add_extensions(browser) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 368

def add_extensions(browser)
  extensions = case browser
               when :chrome, :chrome_headless_shell, :msedge, :microsoftedge
                 Chromium::Driver::EXTENSIONS
               when :firefox
                 Firefox::Driver::EXTENSIONS
               when :safari, :safari_technology_preview
                 Safari::Driver::EXTENSIONS
               when :ie, :internet_explorer
                 IE::Driver::EXTENSIONS
               else
                 []
               end
  extensions.each { |extension| extend extension }
end

#add_virtual_authenticator(options) ⇒ VirtualAuthenticator

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 267

def add_virtual_authenticator(options)
  bridge.add_virtual_authenticator(options)
end

#all

driver.all(class: 'bar') #=> [#<WebDriver::Element:0x1011c3b88, ...]

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 316

alias all find_elements

#bidi ⇒ BiDi

Returns the ::Selenium::WebDriver BiDi connection for this session.

Raises:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 100

def bidi
  @bridge.bidi
end

#browser

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 336

def browser
  bridge.browser
end

#capabilities

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 340

def capabilities
  bridge.capabilities
end

#close

Close the current window, or the browser if no windows are left.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 202

def close
  bridge&.close
end

#create_bridge(caps:, http_client:) (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 357

def create_bridge(caps:, http_client:)
  klass = caps['webSocketUrl'] ? Remote::BiDiBridge : Remote::Bridge
  klass.new(http_client: http_client).tap do |bridge|
    bridge.create_session(caps)
  end
end

#current_url ⇒ String

Get the URL of the current page

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 163

def current_url
  bridge.url
end

#execute_async_script(script) ⇒ WebDriver::Element, ...

Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window. Unlike executing execute_script (synchronous JavaScript), scripts executed with this method must explicitly signal they are finished by invoking the provided callback. This callback is always injected into the executed function as the last argument.

Parameters:

  • script (String) —

    JavaScript source to execute

  • args (WebDriver::Element, Integer, Float, Boolean, NilClass, String, Array) —

    Arguments to the script. May be empty.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 258

def execute_async_script(script, *)
  bridge.execute_async_script(script, *)
end

#execute_script(script) ⇒ WebDriver::Element, ...

Execute the given JavaScript

Parameters:

  • script (String) —

    JavaScript source to execute

  • args (WebDriver::Element, Integer, Float, Boolean, NilClass, String, Array) —

    Arguments will be available in the given script in the 'arguments' pseudo-array.

Returns:

  • (WebDriver::Element, Integer, Float, Boolean, NilClass, String, Array) —

    The value returned from the script.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 239

def execute_script(script, *)
  bridge.execute_script(script, *)
end

#first

driver.first(id: 'foo')

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 310

alias first find_element

#get(url)

Opens the specified URL in the browser.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 153

def get(url)
  navigate.to(url)
end

#inspect

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 78

def inspect
  format '#<%<class>s:0x%<hash>x browser=%<browser>s>', class: self.class, hash: hash * 2,
                                                        browser: bridge.browser.inspect
end

#install_web_extension ⇒ WebExtension

Note:

Chromium requires a BiDi session and installs only unpacked directories (SeleniumHQ/selenium#16541); Firefox falls back to the classic endpoint without BiDi.

Installs a browser extension from an unpacked directory, a packed extension (.xpi/.crx/.zip), or base64-encoded bytes; works with remote (Grid) sessions.

Parameters:

  • path (String) —

    directory, packed extension, or base64-encoded bytes

Returns:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 290

def install_web_extension(...)
  bridge.install_web_extension(...)
end

#manage ⇒ Manager

See Also:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 136

def manage
  bridge.manage
end

#network ⇒ Network

See Also:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 276

def network
  @network ||= WebDriver::Network.new(bridge)
end

#page_source ⇒ String

Get the source of the current page

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 183

def page_source
  bridge.page_source
end

#quit

Quit the browser

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 191

def quit
  bridge.quit
ensure
  @service_manager&.stop
  @devtools&.each_value(&:close)
end

#ref

See Also:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 349

def ref
  [:driver, nil]
end

#screenshot (private)

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 364

def screenshot
  bridge.screenshot
end

#script ⇒ Script

See Also:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 118

def script
  @script ||= WebDriver::Script.new(bridge)
end

#status ⇒ Hash

information about whether a remote end is in a state in which it can create new sessions, and may include additional meta information.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 89

def status
  @bridge.status
end

#switch_to ⇒ TargetLocator

See Also:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 127

def switch_to
  @switch_to ||= WebDriver::TargetLocator.new(bridge)
end

#title ⇒ String

Get the title of the current page

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 173

def title
  bridge.title
end

#uninstall_web_extension(extension)

Uninstalls a browser extension installed with #install_web_extension.

Parameters:

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 300

def uninstall_web_extension(extension)
  bridge.uninstall_web_extension(extension.id)
end

#window_handle ⇒ String

Get the current window handle

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 223

def window_handle
  bridge.window_handle
end

#window_handles ⇒ Array

Get the window handles of open browser windows.

[ GitHub ]

  
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 213

def window_handles
  bridge.window_handles
end