Class WebDriver
A base class representing a driver for a web browser.
public class WebDriver : IWebDriver, IDisposable, ISearchContext, IJavaScriptExecutor, IFindsElement, ITakesScreenshot, ISupportsPrint, IActionExecutor, IAllowsFileDetection, IHasCapabilities, IHasCommandExecutor, IHasSessionId, ICustomDriverCommandExecutor, IHasVirtualAuthenticator
- Inheritance
-
WebDriver
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
WebDriver(ICommandExecutor, ICapabilities)
Initializes a new instance of the WebDriver class.
protected WebDriver(ICommandExecutor executor, ICapabilities capabilities)
Parameters
executor
ICommandExecutorThe ICommandExecutor object used to execute commands.
capabilities
ICapabilitiesThe ICapabilities object used to configuer the driver session.
Fields
DefaultCommandTimeout
The default command timeout for HTTP requests in a RemoteWebDriver instance.
protected static readonly TimeSpan DefaultCommandTimeout
Field Value
Properties
AuthenticatorId
Gets the cached virtual authenticator ID, or null if no authenticator ID is set.
public string? AuthenticatorId { get; }
Property Value
Capabilities
Gets the ICapabilities that the driver session was created with, which may be different from those requested.
public ICapabilities Capabilities { get; }
Property Value
CommandExecutor
Gets the ICommandExecutor which executes commands for this driver.
public ICommandExecutor CommandExecutor { get; }
Property Value
CurrentWindowHandle
Gets the current window handle, which is an opaque handle to this window that uniquely identifies it within this driver instance.
public string CurrentWindowHandle { get; }
Property Value
ElementFactory
Gets or sets the factory object used to create instances of WebElement or its subclasses.
protected WebElementFactory ElementFactory { get; set; }
Property Value
FileDetector
Gets or sets the IFileDetector responsible for detecting sequences of keystrokes representing file paths and names.
public virtual IFileDetector FileDetector { get; set; }
Property Value
IsActionExecutor
Gets a value indicating whether this object is a valid action executor.
public bool IsActionExecutor { get; }
Property Value
PageSource
Gets the source of the page last loaded by the browser.
public string PageSource { get; }
Property Value
SessionId
Gets the SessionId for the current session of this driver.
public SessionId SessionId { get; }
Property Value
Title
Gets the title of the current browser window.
public string Title { get; }
Property Value
Url
Gets or sets the URL the browser is currently displaying.
public string Url { get; set; }
Property Value
- See Also
WindowHandles
Gets the window handles of open browser windows.
public ReadOnlyCollection<string> WindowHandles { get; }
Property Value
Methods
AddCredential(Credential)
Add a credential to the Virtual Authenticator/
public void AddCredential(Credential credential)
Parameters
credential
CredentialThe credential to be stored in the Virtual Authenticator
Exceptions
- ArgumentNullException
If
credential
is null.- InvalidOperationException
If a Virtual Authenticator has not been added yet.
AddVirtualAuthenticator(VirtualAuthenticatorOptions)
Creates a Virtual Authenticator.
public string AddVirtualAuthenticator(VirtualAuthenticatorOptions options)
Parameters
Returns
- string
Authenticator id as string
Exceptions
- ArgumentNullException
If
options
is null.
Close()
Closes the Browser
public void Close()
Dispose()
Dispose the WebDriver Instance
public void Dispose()
Dispose(bool)
Stops the client from running
protected virtual void Dispose(bool disposing)
Parameters
disposing
boolif its in the process of disposing
Execute(string, Dictionary<string, object>)
Executes a command with this driver.
protected virtual Response Execute(string driverCommandToExecute, Dictionary<string, object> parameters)
Parameters
driverCommandToExecute
stringA DriverCommand value representing the command to execute.
parameters
Dictionary<string, object>A Dictionary<TKey, TValue> containing the names and values of the parameters of the command.
Returns
- Response
A Response containing information about the success or failure of the command and any data returned by the command.
ExecuteAsync(string, Dictionary<string, object>)
Executes a command with this driver.
protected virtual Task<Response> ExecuteAsync(string driverCommandToExecute, Dictionary<string, object> parameters)
Parameters
driverCommandToExecute
stringA DriverCommand value representing the command to execute.
parameters
Dictionary<string, object>A Dictionary<TKey, TValue> containing the names and values of the parameters of the command.
Returns
- Task<Response>
A Response containing information about the success or failure of the command and any data returned by the command.
ExecuteAsyncScript(string, params object[])
Executes JavaScript "asynchronously" in the context of the currently selected frame or window, executing the callback function specified as the last argument in the list of arguments.
public object ExecuteAsyncScript(string script, params object[] args)
Parameters
Returns
- object
The value returned by the script.
ExecuteCustomDriverCommand(string, Dictionary<string, object>)
Executes a command with this driver.
public object ExecuteCustomDriverCommand(string driverCommandToExecute, Dictionary<string, object> parameters)
Parameters
driverCommandToExecute
stringThe name of the command to execute. The command name must be registered with the command executor, and must not be a command name known to this driver type.
parameters
Dictionary<string, object>A Dictionary<TKey, TValue> containing the names and values of the parameters of the command.
Returns
- object
A Response containing information about the success or failure of the command and any data returned by the command.
ExecuteScript(PinnedScript, params object[])
Executes JavaScript in the context of the currently selected frame or window
public object ExecuteScript(PinnedScript script, params object[] args)
Parameters
script
PinnedScriptA PinnedScript object containing the JavaScript code to execute.
args
object[]The arguments to the script.
Returns
- object
The value returned by the script.
Exceptions
- ArgumentNullException
If
script
is null.
ExecuteScript(string, params object[])
Executes JavaScript in the context of the currently selected frame or window
public object ExecuteScript(string script, params object[] args)
Parameters
Returns
- object
The value returned by the script.
ExecuteScriptCommand(string, string, params object[])
Executes JavaScript in the context of the currently selected frame or window using a specific command.
protected object ExecuteScriptCommand(string script, string commandName, params object[] args)
Parameters
script
stringThe JavaScript code to execute.
commandName
stringThe name of the command to execute.
args
object[]The arguments to the script.
Returns
- object
The value returned by the script.
FindElement(By)
Finds the first element in the page that matches the By object
public IWebElement FindElement(By by)
Parameters
by
ByBy mechanism to find the object
Returns
- IWebElement
IWebElement object so that you can interact with that object
Examples
IWebDriver driver = new InternetExplorerDriver();
IWebElement elem = driver.FindElement(By.Name("q"));
Exceptions
- ArgumentNullException
If
by
is null.
FindElement(string, string)
Finds an element matching the given mechanism and value.
public virtual IWebElement FindElement(string mechanism, string value)
Parameters
mechanism
stringThe mechanism by which to find the element.
value
stringThe value to use to search for the element.
Returns
- IWebElement
The first IWebElement matching the given criteria.
FindElements(By)
Finds the elements on the page by using the By object and returns a ReadOnlyCollection of the Elements on the page
public ReadOnlyCollection<IWebElement> FindElements(By by)
Parameters
by
ByBy mechanism to find the element
Returns
- ReadOnlyCollection<IWebElement>
ReadOnlyCollection of IWebElement
Examples
IWebDriver driver = new InternetExplorerDriver();
ReadOnlyCollection<IWebElement> classList = driver.FindElements(By.ClassName("class"));
FindElements(string, string)
Finds all elements matching the given mechanism and value.
public virtual ReadOnlyCollection<IWebElement> FindElements(string mechanism, string value)
Parameters
mechanism
stringThe mechanism by which to find the elements.
value
stringThe value to use to search for the elements.
Returns
- ReadOnlyCollection<IWebElement>
A collection of all of the IWebElements matching the given criteria.
GetCapabilitiesDictionary(ICapabilities)
Gets the capabilities as a dictionary.
protected virtual Dictionary<string, object> GetCapabilitiesDictionary(ICapabilities capabilitiesToConvert)
Parameters
capabilitiesToConvert
ICapabilitiesThe dictionary to return.
Returns
- Dictionary<string, object>
A Dictionary consisting of the capabilities requested.
Remarks
This method is only transitional. Do not rely on it. It will be removed once browser driver capability formats stabilize.
GetCredentials()
Retrieves all the credentials stored in the Virtual Authenticator
public List<Credential> GetCredentials()
Returns
- List<Credential>
List of credentials
Exceptions
- InvalidOperationException
If a Virtual Authenticator has not been added yet.
GetScreenshot()
Gets a Screenshot object representing the image of the page on the screen.
public Screenshot GetScreenshot()
Returns
- Screenshot
A Screenshot object containing the image.
Manage()
Instructs the driver to change its settings.
public IOptions Manage()
Returns
Navigate()
Instructs the driver to navigate the browser to another location.
public INavigation Navigate()
Returns
- INavigation
An INavigation object allowing the user to access the browser's history and to navigate to a given URL.
PerformActions(IList<ActionSequence>)
Performs the specified list of actions with this action executor.
public void PerformActions(IList<ActionSequence> actionSequenceList)
Parameters
actionSequenceList
IList<ActionSequence>The list of action sequences to perform.
Print(PrintOptions)
Gets a PrintDocument object representing a PDF-formatted print representation of the page.
public PrintDocument Print(PrintOptions printOptions)
Parameters
printOptions
PrintOptionsA PrintOptions object describing the options of the printed document.
Returns
- PrintDocument
The PrintDocument object containing the PDF-formatted print representation of the page.
Exceptions
- ArgumentNullException
If
printOptions
is null.
Quit()
Close the Browser and Dispose of WebDriver
public void Quit()
RegisterCustomDriverCommand(string, CommandInfo)
Registers a command to be executed with this driver instance.
public bool RegisterCustomDriverCommand(string commandName, CommandInfo commandInfo)
Parameters
commandName
stringThe unique name of the command to register.
commandInfo
CommandInfoThe CommandInfo object describing the command.
Returns
RegisterCustomDriverCommands(IReadOnlyDictionary<string, CommandInfo>)
Registers a set of commands to be executed with this driver instance.
public void RegisterCustomDriverCommands(IReadOnlyDictionary<string, CommandInfo> commands)
Parameters
commands
IReadOnlyDictionary<string, CommandInfo>An IReadOnlyDictionary<TKey, TValue> where the keys are the names of the commands to register, and the values are the CommandInfo objects describing the commands.
RegisterInternalDriverCommand(string, CommandInfo)
Registers a command to be executed with this driver instance as an internally known driver command.
protected bool RegisterInternalDriverCommand(string commandName, CommandInfo commandInfo)
Parameters
commandName
stringThe unique name of the command to register.
commandInfo
CommandInfoThe CommandInfo object describing the command.
Returns
RemoveAllCredentials()
Removes all the credentials stored in the Virtual Authenticator.
public void RemoveAllCredentials()
Exceptions
- InvalidOperationException
If a Virtual Authenticator has not been added yet.
RemoveCredential(byte[])
Removes the credential identified by the credentialId from the Virtual Authenticator.
public void RemoveCredential(byte[] credentialId)
Parameters
credentialId
byte[]The id as byte array that uniquely identifies a credential
Exceptions
- ArgumentNullException
If
credentialId
is null.- InvalidOperationException
If a Virtual Authenticator has not been added yet.
RemoveCredential(string)
Removes the credential identified by the credentialId from the Virtual Authenticator.
public void RemoveCredential(string credentialId)
Parameters
credentialId
stringThe id as string that uniquely identifies a credential
Exceptions
- ArgumentNullException
If
credentialId
is null.- InvalidOperationException
If a Virtual Authenticator has not been added yet.
RemoveVirtualAuthenticator(string)
Removes the Virtual Authenticator
public void RemoveVirtualAuthenticator(string authenticatorId)
Parameters
authenticatorId
stringId as string that uniquely identifies a Virtual Authenticator.
Exceptions
- ArgumentNullException
If
authenticatorId
is null.
ResetInputState()
Resets the input state of the action executor.
public void ResetInputState()
SetUserVerified(bool)
Sets the isUserVerified property for the Virtual Authenticator.
public void SetUserVerified(bool verified)
Parameters
verified
boolThe boolean value representing value to be set
StartSession(ICapabilities)
Starts a session with the driver
protected void StartSession(ICapabilities capabilities)
Parameters
capabilities
ICapabilitiesCapabilities of the browser
SwitchTo()
Method to give you access to switch frames and windows
public ITargetLocator SwitchTo()
Returns
- ITargetLocator
Returns an Object that allows you to Switch Frames and Windows
Examples
IWebDriver driver = new InternetExplorerDriver();
driver.SwitchTo().Frame("FrameName");