Install Selenium WebDriver and browser-specific drivers (e.g., ChromeDriver) using NuGet Package Manager in Visual Studio.
Install-Package Selenium.WebDriver
Install-Package Selenium.WebDriver.ChromeDriver
A comprehensive cheat sheet for using Selenium WebDriver with C#, covering setup, basic operations, element interactions, waits, and best practices. Ideal for beginners and intermediate users.
Install Selenium WebDriver and browser-specific drivers (e.g., ChromeDriver) using NuGet Package Manager in Visual Studio.
|
Ensure the correct version of the ChromeDriver is installed to match your Chrome browser version. |
|
Opening a browser: |
|
Navigating to a URL: |
|
Closing the browser: |
|
Maximize window: |
|
Refresh page: |
|
By ID: |
|
By Name: |
|
By XPath: |
|
By CSS Selector: |
|
By Tag Name: |
|
By Class Name: |
|
Finding Multiple Elements: |
|
Clicking an element: |
|
Sending keys (typing text): |
|
Clearing text: |
|
Getting an attribute value: |
|
Getting text of an element: |
|
Implicit waits tell WebDriver to wait for a certain amount of time when trying to find an element if it is not immediately available.
Note: Avoid mixing implicit and explicit waits. |
Explicit waits allow you to wait for a certain condition to be met before proceeding.
|
Common
|
Switching to an alert: |
|
Accepting an alert: |
|
Dismissing an alert: |
|
Getting alert text: |
|
Switching to an iframe by index: |
|
Switching to an iframe by name or ID: |
|
Switching back to the main document: |
|
Hovering over an element: |
|
Right-clicking an element: |
|
Drag and drop: |
|
Sending keyboard actions: |
|
Taking a screenshot:
|
Basic NUnit setup:
|
|
The Page Object Model (POM) is a design pattern that creates an object repository for web elements. Each page in the application has its own class, which defines the elements and methods to interact with them. Benefits:
|
Example:
|
Store configuration settings (e.g., browser type, base URL) in a configuration file (e.g.,
|
|