Class: Selenium::WebDriver::Driver
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
TakesScreenshot ,
SearchContext
|
|
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
Class Method Summary
- .for(browser, opts = {}) ⇒ Driver Internal use only Internal use only
-
.new(bridge: nil, listener: nil, **opts) ⇒ Driver
constructor
Internal use only
Internal use only
A new
Driver
instance with the given bridge.
Instance Attribute Summary
- #bridge readonly private
Instance Method Summary
-
#[](sel) ⇒ WebDriver::Element
Get the first element matching the given selector.
- #action(**opts) ⇒ ActionBuilder
- #add_virtual_authenticator(options) ⇒ VirtualAuthenticator
-
#all
driver.all(class: ‘bar’) #=> [#<WebDriver::Element:0x1011c3b88, …].
- #browser
- #capabilities
-
#close
Close the current window, or the browser if no windows are left.
-
#current_url ⇒ String
Get the URL of the current page.
-
#execute_async_script(script, *args) ⇒ WebDriver::Element, ...
Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.
-
#execute_script(script, *args) ⇒ WebDriver::Element, ...
(also: #script)
Execute the given JavaScript.
-
#first
driver.first(id: ‘foo’).
-
#get(url)
Opens the specified URL in the browser.
- #inspect
- #manage ⇒ Manager
- #navigate ⇒ Navigation
-
#page_source ⇒ String
Get the source of the current page.
-
#quit
Quit the browser.
-
#script(script, *args)
Alias for #execute_script.
-
#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.
- #switch_to ⇒ TargetLocator
-
#title ⇒ String
Get the title of the current page.
-
#window_handle ⇒ String
Get the current window handle.
-
#window_handles ⇒ Array
Get the window handles of open browser windows.
- #add_extensions(browser) private
- #create_bridge(caps:, url:, http_client: nil) private
- #screenshot private
- #service_url(service) private
- #ref Internal use only Internal use only
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, **opts) ⇒ Driver
A new Driver
instance with the given bridge. End users should use Selenium::WebDriver.for instead of using this directly.
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 71
def initialize(bridge: nil, listener: nil, **opts) @devtools = nil @bidi = nil bridge ||= create_bridge(**opts) add_extensions(bridge.browser) @bridge = listener ? Support::EventFiringBridge.new(bridge, listener) : bridge end
Class Method Details
.for(browser, opts = {}) ⇒ Driver
# 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 313
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>
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 288
def [](sel) sel = {id: sel} if sel.is_a?(String) || sel.is_a?(Symbol) find_element sel end
#action(**opts) ⇒ ActionBuilder
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 126
def action(**opts) bridge.action(**opts) end
#add_extensions(browser) (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/driver.rb', line 330
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
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 252
def add_virtual_authenticator( ) bridge.add_virtual_authenticator( ) end
#all
driver.all(class: ‘bar’) #=> [#<WebDriver::Element:0x1011c3b88, …]
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 268
alias all find_elements
#browser
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/driver.rb', line 294
def browser bridge.browser end
#capabilities
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/driver.rb', line 298
def capabilities bridge.capabilities end
#close
Close the current window, or the browser if no windows are left.
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 184
def close # If no top-level browsing contexts are open after calling close, # it indicates that the WebDriver session is closed. # If the WebDriver session is closed, the BiDi session also needs to be closed. bridge.close.tap { |handles| @bidi&.close if handles&.empty? } end
#create_bridge(caps:, url:, http_client: nil) (private)
[ GitHub ]
#current_url ⇒ String
Get the URL of the current page
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 144
def current_url bridge.url end
#execute_async_script(script, *args) ⇒ 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.
#execute_script(script, *args) ⇒ WebDriver::Element, ... Also known as: #script
Execute the given JavaScript
#first
driver.first(id: ‘foo’)
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 262
alias first find_element
#get(url)
Opens the specified URL in the browser.
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 134
def get(url) navigate.to(url) end
#inspect
[ GitHub ]#manage ⇒ Manager
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 117
def manage bridge.manage end
#page_source ⇒ String
Get the source of the current page
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 164
def page_source bridge.page_source end
#quit
Quit the browser
#ref
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 307
def ref [:driver, nil] end
#screenshot (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/driver.rb', line 326
def screenshot bridge.screenshot end
#script(script, *args)
Alias for #execute_script.
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 274
alias script execute_script
#service_url(service) (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/common/driver.rb', line 321
def service_url(service) @service_manager = service.launch @service_manager.uri 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.
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 90
def status @bridge.status end
#switch_to ⇒ TargetLocator
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 108
def switch_to @switch_to ||= WebDriver::TargetLocator.new(bridge) end
#title ⇒ String
Get the title of the current page
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 154
def title bridge.title end
#window_handle ⇒ String
Get the current window handle
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 208
def window_handle bridge.window_handle end
#window_handles ⇒ Array
Get the window handles of open browser windows.
# File 'rb/lib/selenium/webdriver/common/driver.rb', line 198
def window_handles bridge.window_handles end