Get browser information
Get title
You can read the current page title from the browser:
Get current URL
You can read the current URL from the browser’s address bar using:
This is the multi-page printable view of this section. Click here to print.
You can read the current page title from the browser:
You can read the current URL from the browser’s address bar using:
The first thing you will want to do after launching a browser is to open your website. This can be achieved in a single line:
Pressing the browser’s back button:
Pressing the browser’s forward button:
Refresh the current page:
WebDriver provides an API for working with the three types of native popup messages offered by JavaScript. These popups are styled by the browser and offer limited customisation.
The simplest of these is referred to as an alert, which shows a custom message, and a single button which dismisses the alert, labelled in most browsers as OK. It can also be dismissed in most browsers by pressing the close button, but this will always do the same thing as the OK button. See an example alert.
WebDriver can get the text from the popup and accept or dismiss these alerts.
A confirm box is similar to an alert, except the user can also choose to cancel the message. See a sample confirm.
This example also shows a different approach to storing an alert:
Prompts are similar to confirm boxes, except they also include a text input. Similar to working with form elements, you can use WebDriver’s send keys to fill in a response. This will completely replace the placeholder text. Pressing the cancel button will not submit any text. See a sample prompt.
A cookie is a small piece of data that is sent from a website and stored in your computer. Cookies are mostly used to recognise the user and load the stored information.
WebDriver API provides a way to interact with cookies with built-in methods:
It is used to add a cookie to the current browsing context. Add Cookie only accepts a set of defined serializable JSON object. Here is the link to the list of accepted JSON key values
First of all, you need to be on the domain that the cookie will be valid for. If you are trying to preset cookies before you start interacting with a site and your homepage is large / takes a while to load an alternative is to find a smaller page on the site (typically the 404 page is small, e.g. http://example.com/some404page)
It returns the serialized cookie data matching with the cookie name among all associated cookies.
It returns a ‘successful serialized cookie data’ for current browsing context. If browser is no longer available it returns error.
It deletes the cookie data matching with the provided cookie name.
It deletes all the cookies of the current browsing context.
It allows a user to instruct browsers to control whether cookies are sent along with the request initiated by third party sites. It is introduced to prevent CSRF (Cross-Site Request Forgery) attacks.
Same-Site cookie attribute accepts two parameters as instructions
When the sameSite attribute is set as Strict, the cookie will not be sent along with requests initiated by third party websites.
When you set a cookie sameSite attribute to Lax, the cookie will be sent along with the GET request initiated by third party website.
Note: As of now this feature is landed in chrome(80+version), Firefox(79+version) and works with Selenium 4 and later versions.
Frames are a now deprecated means of building a site layout from multiple documents on the same domain. You are unlikely to work with them unless you are working with an pre HTML5 webapp. Iframes allow the insertion of a document from an entirely different domain, and are still commonly used.
If you need to work with frames or iframes, WebDriver allows you to work with them in the same way. Consider a button within an iframe. If we inspect the element using the browser development tools, we might see the following:
If it was not for the iframe we would expect to click on the button using something like:
However, if there are no buttons outside of the iframe, you might instead get a no such element error. This happens because Selenium is only aware of the elements in the top level document. To interact with the button, we will need to first switch to the frame, in a similar way to how we switch windows. WebDriver offers three ways of switching to a frame.
Switching using a WebElement is the most flexible option. You can find the frame using your preferred selector and switch to it.
If your frame or iframe has an id or name attribute, this can be used instead. If the name or ID is not unique on the page, then the first one found will be switched to.
It is also possible to use the index of the frame, such as can be queried using window.frames in JavaScript.
To leave an iframe or frameset, switch back to the default content like so:
WebDriver does not make the distinction between windows and tabs. If your site opens a new tab or window, Selenium will let you work with it using a window handle. Each window has a unique identifier which remains persistent in a single session. You can get the window handle of the current window by using:
Clicking a link which opens in a new window will focus the new window or tab on screen, but WebDriver will not know which window the Operating System considers active. To work with the new window you will need to switch to it. For this, we fetch all window handles, and store them in an array. The array position fills in the order the window is launched. So first position will be default browser, and so on.
When you are finished with a window or tab and it is not the last window or tab open in your browser, you should close it and switch back to the window you were using previously. Assuming you followed the code sample in the previous section you will have the previous window handle stored in a variable. Put this together and you will get:
Forgetting to switch back to another window handle after closing a window will leave WebDriver executing on the now closed page, and will trigger a No Such Window Exception. You must switch back to a valid window handle in order to continue execution.
Creates a new window (or) tab and will focus the new window or tab on screen. You don’t need to switch to work with the new window (or) tab. If you have more than two windows (or) tabs opened other than the new window, you can loop over both windows or tabs that WebDriver can see, and switch to the one which is not the original.
Note: This feature works with Selenium 4 and later versions.
Opens a new tab and switches to new tab:
Opens a new window and switches to new window:
When you are finished with the browser session you should call quit, instead of close:
Failure to call quit will leave extra background processes and ports running on your machine which could cause you problems later.
Some test frameworks offer methods and annotations which you can hook into to tear down at the end of a test.
If not running WebDriver in a test context, you may consider using
try / finally
which is offered by most languages so that an exception
will still clean up the WebDriver session.
Python’s WebDriver now supports the python context manager,
which when using the with
keyword can automatically quit the driver at
the end of execution.
Screen resolution can impact how your web application renders, so WebDriver provides mechanisms for moving and resizing the browser window.
Fetches the size of the browser window in pixels.
Restores the window and sets the window size.
Fetches the coordinates of the top left coordinate of the browser window.
Moves the window to the chosen position.
Enlarges the window. For most operating systems, the window will fill the screen, without blocking the operating system’s own menus and toolbars.
Minimizes the window of current browsing context. The exact behavior of this command is specific to individual window managers.
Minimize Window typically hides the window in the system tray.
Note: This feature works with Selenium 4 and later versions.
Fills the entire screen, similar to pressing F11 in most browsers.
Used to capture screenshot for current browsing context. The WebDriver endpoint screenshot returns screenshot which is encoded in Base64 format.
Used to capture screenshot of an element for current browsing context. The WebDriver endpoint screenshot returns screenshot which is encoded in Base64 format.
Executes JavaScript code snippet in the current context of a selected frame or window.
Prints the current page within the browser.
Note: This requires Chromium Browsers to be in headless mode
Web applications can enable a public key-based authentication mechanism known as Web Authentication to authenticate users in a passwordless manner. Web Authentication defines APIs that allows a user to create a public-key credential and register it with an authenticator. An authenticator can be a hardware device or a software entity that stores user’s public-key credentials and retrieves them on request.
As the name suggests, Virtual Authenticator emulates such authenticators for testing.
A Virtual Authenticatior has a set of properties. These properties are mapped as VirtualAuthenticatorOptions in the Selenium bindings.
It creates a new virtual authenticator with the provided properties.
Removes the previously added virtual authenticator.
Creates a resident (stateful) credential with the given required credential parameters.
Creates a resident (stateless) credential with the given required credential parameters.
Registers the credential with the authenticator.
Returns the list of credentials owned by the authenticator.
Removes a credential from the authenticator based on the passed credential id.
Removes all the credentials from the authenticator.
Sets whether the authenticator will simulate success or fail on user verification.
Learn more or view the full list of sponsors.