Selenium is an open-source, cross-platform framework that allows you to automate web browsers. It provides a way to interact with web elements, perform actions, and validate the behavior of web applications, just like a real user would. Selenium supports various programming languages, including Java, Python, C#, and more, making it versatile and accessible to a broad range of developers and testers.
Selenium is composed of several key components:
- Selenium WebDriver: The core component for automating web browsers. It provides a programming interface for interacting with web elements and browsers.
- Selenium IDE: A record-and-playback tool for creating simple automation scripts without coding. Useful for beginners and quick test case creation.
- Selenium Grid: Allows you to distribute test execution across multiple machines, browsers, and platforms simultaneously. Ideal for running tests in parallel to save time.
- ID
- Name
- Class Name
- Tag Name
- Link Text
- Partial Link Text
- CSS Selector
- XPath
Java
C#
Python
Javascript
Kotlin
// Find element by id
By.id("element_id");
// Find element name
By.name("name");
// Find element by class name
By.className("class_name");
// Find element tag name
By.tagName("tag_name");
// Find element link text
By.linkText("link_text");
// Find element by partial link text
By.partialLinkText("partial_link_text");
// Find element by css selector
By.cssSelector("#css_selector");
// Find element by xpath
By.xpath("//xpath");
// Find element by id
By.Id("element_id");
// Find element name
By.Name("name");
// Find element by class name
By.ClassName("class_name");
// Find element tag name
By.TagName("tag_name");
// Find element link text
By.LinkText("link_text");
// Find element by partial link text
By.PartialLinkText("partial_link_text");
// Find element by css selector
By.CssSelector("#css_selector");
// Find element by xpath
By.XPath("//xpath");
# Find element by id
driver.find_element(By.ID, "element_id")
# Find element by name
driver.find_element(By.NAME, "name")
# Find element by class name
driver.find_element(By.CLASS_NAME, "class_name")
# Find element by tag name
driver.find_element(By.TAG_NAME, "tag_name")
# Find element by link text
driver.find_element(By.LINK_TEXT, "link_text")
# Find element by partial link text
driver.find_element(By.PARTIAL_LINK_TEXT, "partial_link_text")
# Find element by css selector
driver.find_element(By.CSS_SELECTOR, "#elementid")
# Find element by xpath
driver.find_element(By.XPATH, "//xpath")
// Find element by id
By.id("elementId")
// Find element name
By.name("name")
// Find element by class name
By.className("className")
// Find element tag name
By.tagName("tagName")
// Find element link text
By.linkText("linkText")
// Find element by partial link text
By.partialLinkText("partialLinkText")
// Find element by css selector
By.css("cssSelector")
// Find element by xpath
By.xpath("xpath")
// Find element by id
By.id("element_id");
// Find element name
By.name("name");
// Find element by class name
By.className("class_name");
// Find element tag name
By.tagName("tag_name");
// Find element link text
By.linkText("link_text");
// Find element by partial link text
By.partialLinkText("partial_link_text");
// Find element by css selector
By.cssSelector("#css_selector");
// Find element by xpath
By.xpath("//xpath");
Java
C#
Python
Javascript
Kotlin
//Load a new web page in the current browser window..
driver.get("https://example.com");
//It will close the current window.
driver.close();
//This method will return the title of the current page.
driver.getTitle();
//It will return the URL of the page currently loaded in the browser.
driver.getCurrentUrl();
//Get the source of the last loaded page.
driver.getPageSource();
//Quits this driver, closing every associated window.
driver.quit();
//Load a new web page in the current browser window..
driver.Url = "https://example.com";
//It will close the current window.
driver.Close();
//This method will return the title of the current page.
String pageTitle = driver.Title;
//It will return the URL of the page currently loaded in the browser.
String currentUrl = driver.Url;
//Get the source of the last loaded page.
String pageSource = driver.PageSource;
//Quits this driver, closing every associated window.
driver.Quit();
# Load a new web page in the current browser window..
driver.get("https://example.com")
# It will close the current window.
driver.close()
# This method will return the title of the current page.
title = driver.title
# It will return the URL of the page currently loaded in the browser.
current_url = driver.current_url
# Get the source of the last loaded page.
page_source = driver.page_source
# Quits this driver, closing every associated window.
driver.quit()
//Load a new web page in the current browser window..
await driver.get("https://example.com");
//It will close the current window.
await driver.close();
//This method will return the title of the current page.
await driver.getTitle();
//It will return the URL of the page currently loaded in the browser.
await driver.getCurrentUrl();
//Get the source of the last loaded page.
await driver.getPageSource();
//Quits this driver, closing every associated window.
driver.quit();
//Load a new web page in the current browser window..
driver.get("https://example.com");
//It will close the current window.
driver.close();
//This method will return the title of the current page.
driver.title;
//It will return the URL of the page currently loaded in the browser.
driver.currentUrl;
//Get the source of the last loaded page.
driver.pageSource;
//Quits this driver, closing every associated window.
driver.quit();
Java
C#
Python
Javascript
Kotlin
//Load a new web page in the current browser window.
driver.navigate().to("http://www.example2.com");
// Move back a single "item" in the browser's history.
driver.navigate().back();
// Move a single "item" forward in the browser's history.
driver.navigate().forward();
// Refresh the current page
driver.navigate().refresh();
//Load a new web page in the current browser window.
driver.Navigate().GoToUrl("http://www.example2.com");
// Move back a single "item" in the browser's history.
driver.Navigate().Back();
// Move a single "item" forward in the browser's history.
driver.Navigate().Forward();
// Refresh the current page
driver.Navigate().Refresh();
# Load a new web page in the current browser window.
driver.get("http://www.example2.com")
# Move back a single "item" in the browser's history.
driver.back()
# Move a single "item" forward in the browser's history.
driver.forward()
# Refresh the current page
driver.refresh()
//Load a new web page in the current browser window.
await driver.navigate().to("http://www.example2.com");
// Move back a single "item" in the browser's history.
await driver.navigate().back()
// Move a single "item" forward in the browser's history.
await driver.navigate().forward()
// Refresh the current page
await driver.navigate().refresh()
//Load a new web page in the current browser window.
driver.navigate().to("http://www.example2.com");
// Move back a single "item" in the browser's history.
driver.navigate().back()
// Move a single "item" forward in the browser's history.
driver.navigate().forward()
// Refresh the current page
driver.navigate().refresh()
Java
C#
Python
Javascript
Kotlin
//Fullscreen the current window if it is not already fullscreen
driver.manage().window().fullscreen();
// Maximizes the current window if it is not already maximized
driver.manage().window().maximize();
// Minimizes the current window if it is not already minimized
driver.manage().window().minimize();
//This method will return the size of the current window.
driver.manage().window().getSize();
//This method will set the size of the current window with given parameters.
driver.manage().window().setSize(new Dimension(500, 500));
//Fullscreen the current window if it is not already fullscreen
driver.Manage().Window.FullScreen();
// Maximizes the current window if it is not already maximized
driver.Manage().Window.Maximize();
// Minimizes the current window if it is not already minimized
driver.Manage().Window.Minimize();
//This method will return the size of the current window.
var size = driver.Manage().Window.Size;
//This method will set the size of the current window with given parameters.
driver.Manage().Window.Size = new Size(600, 800);
# Fullscreen the current window if it is not already fullscreen
driver.fullscreen_window()
# Maximizes the current window if it is not already maximized
driver.maximize_window()
# Minimizes the current window if it is not already minimized
driver.minimize_window()
# This method will return the size of the current window.
driver.get_window_size()
# This method will set the size of the current window with given parameters.
driver.set_window_size(200, 200)
//Fullscreen the current window if it is not already fullscreen
await driver.manage().window().fullscreen();
// Maximizes the current window if it is not already maximized
await driver.manage().window().maximize();
// Minimizes the current window if it is not already minimized
await driver.manage().window().minimize();
//This method will return the size of the current window.
await driver.manage().window().getSize();
//This method will set the size of the current window with given parameters.
await driver.manage().window().setSize(800, 600);
//Fullscreen the current window if it is not already fullscreen
driver.manage().window().fullscreen();
// Maximizes the current window if it is not already maximized
driver.manage().window().maximize();
// Minimizes the current window if it is not already minimized
driver.manage().window().minimize();
//This method will return the size of the current window.
var size = driver.manage().window().size;
//This method will set the size of the current window with given parameters.
driver.manage().window().size = Dimension(500, 500)
Java
C#
Python
Javascript
Kotlin
//Sets the wait when searching for an element if it is not immediately present.
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
//Sets the wait for a page load to complete before throwing an error.
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
//Sets the wait for an asynchronous script to finish execution before throwing an error.
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(10));
//Sets the wait when searching for an element if it is not immediately present.
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
//Sets the wait for a page load to complete before throwing an error.
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10);
//Sets the wait for an asynchronous script to finish execution before throwing an error.
driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(10);
# Sets the wait when searching for an element if it is not immediately present.
driver.implicitly_wait(10)
# Sets the wait for a page load to complete before throwing an error.
driver.set_page_load_timeout(10)
# Sets the wait for an asynchronous script to finish execution before throwing an error.
driver.set_script_timeout(10)
//Sets the wait when searching for an element if it is not immediately present.
await driver.manage().setTimeouts({ implicit: 2000 });
//Sets the wait for a page load to complete before throwing an error.
await driver.manage().setTimeouts({ pageLoad: 2000 });
//Sets the wait for an asynchronous script to finish execution before throwing an error.
await driver.manage().setTimeouts({ script: 2000 });
//Sets the wait when searching for an element if it is not immediately present.
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
//Sets the wait for a page load to complete before throwing an error.
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));
//Sets the wait for an asynchronous script to finish execution before throwing an error.
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(10));
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/wait/");
driver.manage().window().maximize();
driver.findElement(By.id("wait1")).click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Elem1StatusA")));
driver.quit();
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/wait/";
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("wait1")).Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(d =>d.FindElement(By.Id("wait1Status")).Text.Equals("Wait 1 Status"));
driver.Quit();
driver = webdriver.Chrome()
driver.get("http://localhost:8080/demo/wait/");
driver.maximize_window()
driver.find_element(By.ID, "wait1").click()
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.ID, "elementId")))
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/wait/");
await driver.manage().window().maximize();
await driver.findElement(By.id("wait1")).click();
const wait = await driver.wait(until.elementIsVisible(driver.findElement(By.id("Elem1StatusA"))), 10000);
await driver.quit();
val driver = ChromeDriver()
driver.get("http://localhost:8080/demo/wait/")
driver.manage().window().maximize()
driver.findElement(By.id("wait1")).click()
val wait = WebDriverWait(driver, Duration.ofSeconds(10))
wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("revealed")) as Function<in WebDriver, WebElement?>
)
driver.quit()
Java
C#
Python
Javascript
Kotlin
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/wait/");
driver.manage().window().maximize();
driver.findElement(By.id("wait1")).click();
FluentWait<WebDriver> wait = new FluentWait<>(driver);
wait.withTimeout(Duration.ofSeconds(10));
wait.pollingEvery(Duration.ofMillis(500));
wait.ignoring(NoSuchElementException.class);
wait.until(d-> d.findElement(By.id("Elem1StatusA")).isDisplayed());
driver.quit();
IWebDriver driver = new ChromeDriver();
driver.Url = "http://localhost:8080/demo/wait/";
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("wait1")).Click();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.PollingInterval = TimeSpan.FromSeconds(2);
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
wait.Until(d => d.FindElement(By.Id("wait1Status")).Text.Equals("Wait 1 Status"));
driver.Quit();
driver = webdriver.Chrome()
driver.get("http://localhost:8080/demo/wait/")
driver.maximize_window()
driver.find_element(By.ID, "wait1").click()
wait = WebDriverWait(
driver,
timeout=10,
poll_frequency=1,
ignored_exceptions=[NoSuchElementException]
)
wait.until(EC.visibility_of_element_located((By.ID, "Elem1StatusA")))
driver.quit()
let driver = await new Builder().forBrowser('chrome').build();
await driver.get("http://localhost:8080/demo/wait/");
await driver.manage().window().maximize();
await driver.findElement(By.id("wait1")).click();
let wait = new WebDriverWait(driver, 10000, 500, 'Custom timeout message');
let dynamicElement = await wait.until(async function(driver) {
try
{
let element = await driver.findElement(By.id('Elem1StatusA'));
if (await element.isDisplayed()) {
return element;
}
return null;
}
catch (e)
{
if (e instanceof NoSuchElementError)
{
return null;
}
throw e;
}
});
await dynamicElement;
await driver.quit();
val driver: WebDriver = ChromeDriver()
driver["http://localhost:8080/demo/wait/"]
driver.manage().window().maximize()
driver.findElement(By.id("wait1")).click()
val wait = FluentWait(driver)
wait.withTimeout(Duration.ofSeconds(10))
wait.pollingEvery(Duration.ofMillis(500))
wait.ignoring(NoSuchElementException::class.java)
wait.until { d: WebDriver -> d.findElement(By.id("Elem1StatusA")).isDisplayed }
driver.quit()
Java
C#
Python
Javascript
Kotlin
//Getting first window handle
String firstWindowHandle = driver.getWindowHandle();
//Click on link to open second window
driver.findElement(By.id("page2Link")).click();
//Getting all opened window handles
Set<String> allOpenWindowHandles = driver.getWindowHandles();
//Looping through all window handles to switch to new window handle
for (String windowHandle : allOpenWindowHandles)
{
if(!windowHandle.equals(firstWindowHandle))
{
driver.switchTo().window(windowHandle);
break;
}
}
//Getting first window handle
String firstWindowHandle = driver.CurrentWindowHandle;
//Click on link to open second window
driver.FindElement(By.Id("page2Link")).Click();
//Getting all opened window handles
var allOpenWindowHandles = driver.WindowHandles;
//Looping through all window handles to switch to new window handle
foreach (String windowHandle in allOpenWindowHandles)
{
if (!windowHandle.Equals(firstWindowHandle))
{
driver.SwitchTo().Window(windowHandle);
break;
}
}
# Getting first window handle
firstWindowHandle = driver.current_window_handle
# Click on link to open second window
driver.find_element(By.ID, "page2Link").click()
# Getting all opened window handles
allOpenWindowHandles = driver.window_handles
# Looping through all window handles to switch to new window handle
for windowHandle in allOpenWindowHandles:
if windowHandle != firstWindowHandle:
driver.switch_to.window(windowHandle)
break
//Getting first window handle
let firstWindowHandle = driver.getWindowHandle();
//Click on link to open second window
await driver.findElement(By.id("page2Link")).click();
//Getting all opened window handles
await driver.wait(async () => {
const handles = await driver.getAllWindowHandles();
return handles.length === 2; // Wait until there are exactly 2 handles
}, 5000, 'Timed out waiting for new window to open');
let allWindowHandles = await driver.getAllWindowHandles();
// 5. Iterate through handles and switch to the child window
for (const handle of allWindowHandles)
{
if (handle !== parentWindowHandle)
{
await driver.switchTo().window(handle);
break;
}
}
//Getting first window handle
val firstWindowHandle: String = driver.windowHandle
//Click on link to open second window
driver.findElement(By.id("page2Link")).click()
//Getting all opened window handles
val allOpenWindowHandles: Set<String> = driver.windowHandles
//Looping through all window handles to switch to new window handle
for (windowHandle in allOpenWindowHandles) {
if (windowHandle != firstWindowHandle) {
driver.switchTo().window(windowHandle)
break
}
}
Java
C#
Python
Javascript
Kotlin
// Switch to the frame
driver.switchTo().frame(frame1);
// Switch focus to the parent context.
driver.switchTo().parentFrame();
// Switch to the main document from frame
driver.switchTo().defaultContent();
// Switch to the frame
driver.SwitchTo().Frame("frame1");
// Switch focus to the parent context.
driver.SwitchTo().ParentFrame();
// Switch to the main document from frame
driver.SwitchTo().DefaultContent();
# Switch to the frame
driver.switch_to.frame("frame1")
# Switch focus to the parent context.
driver.switch_to.parent_frame()
# Switch to the main document from frame
driver.switch_to.default_content()
// Switch to the frame
await driver.switchTo().frame(frame1);
// Switch focus to the parent context.
await driver.switchTo().parentFrame();
// Switch to the main document from frame
await driver.switchTo().defaultContent();
// Switch to the frame
driver.switchTo().frame("frame1")
// Switch focus to the parent context.
driver.switchTo().parentFrame()
// Switch to the main document from frame
driver.switchTo().defaultContent()
Java
C#
Python
Javascript
Kotlin
//Switch to alert
Alert alert = driver.switchTo().alert();
// To accept the alert dialog
alert.accept();
// To dismiss the alert dialog
alert.dismiss();
// To get alert dialog text
alert.getText();
// To enter text in prompt dialog
alert.sendKeys("sample");
//Switch to alert
IAlert alert = driver.SwitchTo().Alert();
// To accept the alert dialog
alert.Accept();
// To dismiss the alert dialog
alert.Dismiss();
// To get alert dialog text
String alertText = alert.Text;
// To enter text in prompt dialog
alert.SendKeys("sample");
# Switch to alert
alert = driver.switch_to.alert
# To accept the alert dialog
alert.accept()
# To dismiss the alert dialog
alert.dismiss()
# To get alert dialog text
text = alert.text
# To enter text in prompt dialog
alert.send_keys("sample")
//Switch to alert
let alert = await driver.switchTo().alert();
// To accept the alert dialog
await alert.accept();
// To dismiss the alert dialog
await alert.dismiss();
// To get alert dialog text
await alert.getText();
// To enter text in prompt dialog
await alert.sendKeys("sample");
//Switch to alert
val alert = driver.switchTo().alert()
// To accept the alert dialog
alert.accept()
// To dismiss the alert dialog
alert.dismiss()
// To get alert dialog text
alert.text
// To enter text in prompt dialog
alert.sendKeys("sample")
Java
C#
Python
Javascript
Kotlin
//Find the first WebElement using the given method.
WebElement element = driver.findElement(By.id("some_id"));
//Find all elements within the current page using the given mechanism.
List<WebElement> elements = driver.findElements(By.tagName("some_tag"));
// To type into an element, which may set its value
element.sendKeys("some text");
// Get the visible text of this element
element.getText();
// Check if this element displayed
element.isDisplayed();
// Check if this element enabled
element.isEnabled();
// Get the tag name of this element
element.getTagName();
// Click this element
element.click();
// It will submit the form
element.submit();
// Get the value of the given attribute
element.getAttribute("value");
// Get the value of a given CSS property
element.getCssValue("float");
// Find the first WebElement within element
element.findElement(By.id("someId"));
// Find WebElements within element
element.findElements(By.className("items"));
// It will reset element value
element.clear();
//Find the first WebElement using the given method.
IWebElement element = driver.FindElement(By.Id("some_id"));
//Find all elements within the current page using the given mechanism.
var elements = driver.FindElements(By.TagName("some_tag"));
// To type into an element, which may set its value
element.SendKeys("some text");
// Get the visible text of this element
String text = element.Text;
// Check if this element displayed
bool isDisplayed = element.Displayed;
// Check if this element enabled
bool isEnabled = element.Enabled;
// Get the tag name of this element
String tagName = element.TagName;
// Click this element
element.Click();
// It will submit the form
element.Submit();
// Get the value of the given attribute
element.GetAttribute("value");
// Get the value of a given CSS property
element.GetCssValue("float");
// Find the first WebElement within element
element.FindElement(By.Id("someId"));
// Find WebElements within element
element.FindElements(By.ClassName("items"));
// It will reset element value
element.Clear();
# Find the first WebElement using the given method.
element = driver.find_element(By.ID, "some_id")
# Find all elements within the current page using the given mechanism.
elements = driver.find_elements(By.TAG_NAME, "some_tag")
# To type into an element, which may set its value
element.send_keys("some text")
# Get the visible text of this element
some_text = element.text
# Check if this element displayed
element.is_displayed()
# Check if this element enabled
element.is_enabled()
# Get the tag name of this element
tag_name = element.tag_name
# Click this element
element.click()
# It will submit the form
element.submit()
# Get the value of the given attribute
element.get_attribute("value")
# Get the value of a given CSS property
element.value_of_css_property("float")
# Find the first WebElement within element
element.find_element(By.ID, "someId")
# Find WebElements within element
element.find_elements(By.CLASS_NAME, "items")
# It will reset element value
element.clear()
//Find the first WebElement using the given method.
let element = await driver.findElement(By.id("some_id"));
//Find all elements within the current page using the given mechanism.
let elements = await driver.findElements(By.tagName("some_tag"));
// To type into an element, which may set its value
await element.sendKeys("some text");
// Get the visible text of this element
await element.getText();
// Check if this element displayed
await element.isDisplayed();
// Check if this element enabled
await element.isEnabled();
// Get the tag name of this element
await element.getTagName();
// Click this element
await element.click();
// It will submit the form
await element.submit();
// Get the value of the given attribute
await element.getAttribute("value");
// Get the value of a given CSS property
await element.getCssValue("float");
// Find the first WebElement within element
await element.findElement(By.id("someId"));
// Find WebElements within element
await element.findElements(By.className("items"));
// It will reset element value
await element.clear();
//Find the first WebElement using the given method.
val element = driver.findElement(By.id("some_id"))
//Find all elements within the current page using the given mechanism.
val elements = driver.findElements(By.tagName("some_tag"))
// To type into an element, which may set its value
element.sendKeys("some text")
// Get the visible text of this element
element.text
// Check if this element displayed
element.isDisplayed
// Check if this element enabled
element.isEnabled
// Get the tag name of this element
element.tagName
// Click this element
element.click()
// It will submit the form
element.submit()
// Get the value of the given attribute
element.getAttribute("value")
// Get the value of a given CSS property
element.getCssValue("float")
// Find the first WebElement within element
element.findElement(By.id("someId"))
// Find WebElements within element
element.findElements(By.className("items"))
// It will reset element value
element.clear()
Java
C#
Python
Javascript
Kotlin
Select select = new Select(element);
// Select the option at the given index.
select.selectByIndex(1);
// Select all options that have a value matching the argument.
select.selectByValue("some value");
// Select all options that display text matching the argument.
select.selectByVisibleText("Visible text");
// Clear all selected entries.
select.deselectAll();
// Deselect the option at the given index.
select.deselectByIndex(1);
// Deselect all options that have a value matching the argument.
select.deselectByValue("some value");
// Deselect all options that display text matching the argument.
select.deselectByVisibleText("visible text");
// Returns all selected options belonging to this select tag
select.getAllSelectedOptions();
// Returns the first selected option in this select tag
select.getFirstSelectedOption();
// Returns all options belonging to this select tag
select.getOptions();
// Check whether this select element support selecting multiple options
select.isMultiple();
SelectElement select = new SelectElement(element);
// Select the option at the given index.
select.SelectByIndex(1);
// Select all options that have a value matching the argument.
select.SelectByValue("some value");
// Select all options that display text matching the argument.
select.SelectByText("Visible text");
// Clear all selected entries.
select.DeselectAll();
// Deselect the option at the given index.
select.DeselectByIndex(1);
// Deselect all options that have a value matching the argument.
select.DeselectByValue("some value");
// Deselect all options that display text matching the argument.
select.DeselectByText("visible text");
// Returns all selected options belonging to this select tag
var allSelected = select.AllSelectedOptions;
// Returns the first selected option in this select tag
var firstSelected = select.SelectedOption;
// Returns all options belonging to this select tag
var options = select.Options;
// Check whether this select element support selecting multiple options
bool isMultiple = select.IsMultiple;
element = driver.find_element(By.ID, "elementId")
select = Select(element)
# Select the option at the given index.
select.select_by_index(1)
# Select all options that have a value matching the argument.
select.select_by_value("some value")
# Select all options that display text matching the argument.
select.select_by_visible_text("Visible text")
# Clear all selected entries.
select.deselect_all()
# Deselect the option at the given index.
select.deselect_by_index(1)
# Deselect all options that have a value matching the argument.
select.deselect_by_value("some value")
# Deselect all options that display text matching the argument.
select.deselect_by_visible_text("visible text")
# Returns all selected options belonging to this select tag
selected_options = select.all_selected_options
# Returns the first selected option in this select tag
first_selected = select.first_selected_option
# Returns all options belonging to this select tag
all_options = select.options
# Check whether this select element support selecting multiple options
select.is_multiple()
const select = new Select(selectElement);
// Select the option at the given index.
await select.selectByIndex(1);
// Select all options that have a value matching the argument.
await select.selectByValue("some value");
// Select all options that display text matching the argument.
await select.selectByVisibleText("Visible text");
// Clear all selected entries.
await select.deselectAll();
// Deselect the option at the given index.
await select.deselectByIndex(1);
// Deselect all options that have a value matching the argument.
await select.deselectByValue("some value");
// Deselect all options that display text matching the argument.
await select.deselectByVisibleText("visible text");
// Returns all selected options belonging to this select tag
await select.getAllSelectedOptions();
// Returns the first selected option in this select tag
await select.getFirstSelectedOption();
// Returns all options belonging to this select tag
await select.getOptions();
// Check whether this select element support selecting multiple options
await select.isMultiple();
val element = driver.findElement(By.id("selectElement"))
val select: Select = Select(element)
// Select the option at the given index.
select.selectByIndex(1)
// Select all options that have a value matching the argument.
select.selectByValue("some value")
// Select all options that display text matching the argument.
select.selectByVisibleText("Visible text")
// Clear all selected entries.
select.deselectAll()
// Deselect the option at the given index.
select.deselectByIndex(1)
// Deselect all options that have a value matching the argument.
select.deselectByValue("some value")
// Deselect all options that display text matching the argument.
select.deselectByVisibleText("visible text")
// Returns all selected options belonging to this select tag
select.allSelectedOptions
// Returns the first selected option in this select tag
select.firstSelectedOption
// Returns all options belonging to this select tag
select.options
// Check whether this select element support selecting multiple options
select.isMultiple
Java
C#
Python
Javascript
Kotlin
JavascriptExecutor je = (JavascriptExecutor) driver;
// Executes JavaScript in the context of the currently selected frame or window.
je.executeScript("window.scrollBy(10,500)");
//Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.
je.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 2000);");
IJavaScriptExecutor je = (IJavaScriptExecutor)driver;
// Executes JavaScript in the context of the currently selected frame or window.
je.ExecuteScript("window.scrollBy(10,500)");
//Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.
je.ExecuteAsyncScript("window.setTimeout(arguments[arguments.length - 1], 2000);");
# Executes JavaScript in the context of the currently selected frame or window.
driver.execute_script("window.scrollBy(10,500)")
# Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.
driver.execute_async_script("window.setTimeout(arguments[arguments.length - 1], 2000);")
// Executes JavaScript in the context of the currently selected frame or window.
driver.executeScript("window.scrollBy(10,500)");
//Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.
driver.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 2000);");
val je = driver as JavascriptExecutor
// Executes JavaScript in the context of the currently selected frame or window.
je.executeScript("window.scrollBy(10,500)")
//Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.
je.executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 2000);")
Java
C#
Python
Javascript
Kotlin
// For emulating complex user gestures and keyboard or Mouse actions
Actions actions = new Actions(driver);
// Drang and drop
actions.dragAndDrop(sourceElement, destinationElement);
// Clicks at the current mouse location.
actions.click();
// Clicks (without releasing) at the current mouse location
actions.clickAndHold();
// Performs a context-click at the current mouse location.
actions.contextClick();
// Performs a double-click at the current mouse location.
actions.doubleClick();
// Performs a modifier key press.
actions.keyDown(Keys.ENTER);
// Moves the mouse to the middle of the element.
actions.moveToElement(destinationElement);
// Releases the depressed left mouse button at the current mouse location.
actions.release();
// Sends keys to the active element.
actions.sendKeys();
// For emulating complex user gestures and keyboard or Mouse actions
Actions actions = new Actions(driver);
// Drang and drop
actions.DragAndDrop(sourceElement, destinationElement);
// Clicks at the current mouse location.
actions.Click();
// Clicks (without releasing) at the current mouse location
actions.ClickAndHold();
// Performs a context-click at the current mouse location.
actions.ContextClick();
// Performs a double-click at the current mouse location.
actions.DoubleClick();
// Performs a modifier key press.
actions.KeyDown(Keys.Enter);
// Moves the mouse to the middle of the element.
actions.MoveToElement(destinationElement);
// Releases the depressed left mouse button at the current mouse location.
actions.Release();
// Sends keys to the active element.
actions.SendKeys();
sourceElement = driver.find_element(By.ID, "elementId")
destinationElement = driver.find_element(By.ID, "elementId")
# For emulating complex user gestures and keyboard or Mouse actions
actions = ActionChains(driver)
# Drag and drop
actions.drag_and_drop(sourceElement, destinationElement)
# Clicks at the current mouse location.
actions.click()
# Clicks (without releasing) at the current mouse location
actions.click_and_hold()
# Performs a context-click at the current mouse location.
actions.context_click()
# Performs a double-click at the current mouse location.
actions.double_click()
# Performs a modifier key press.
actions.key_down(Keys.ENTER)
# Moves the mouse to the middle of the element.
actions.move_to_element(destinationElement)
# Releases the depressed left mouse button at the current mouse location.
actions.release()
# Sends keys to the active element.
actions.send_keys()
// Drang and drop
await driver.actions().dragAndDrop(sourceElement, destinationElement).perform();
// Clicks at the current mouse location.
await driver.actions().click().perform();
// Clicks (without releasing) at the current mouse location
await driver.actions().clickAndHold().perform();
// Performs a context-click at the current mouse location.
await driver.actions().contextClick().perform();
// Performs a double-click at the current mouse location.
await driver.actions().doubleClick().perform();
// Performs a modifier key press.
await driver.actions().keyDown(Keys.ENTER).perform();
// Moves the mouse to the middle of the element.
await driver.actions().moveToElement(destinationElement).perform();
// Releases the depressed left mouse button at the current mouse location.
await driver.actions().release().perform();
// Sends keys to the active element.
await driver.actions().sendKeys().perform();
// For emulating complex user gestures and keyboard or Mouse actions
val actions = Actions(driver)
// Drag and drop
val sourceElement = driver.findElement(By.id("srcElem"))
val destinationElement = driver.findElement(By.id("destElem"))
actions.dragAndDrop(sourceElement, destinationElement)
// Clicks at the current mouse location.
actions.click()
// Clicks (without releasing) at the current mouse location
actions.clickAndHold()
// Performs a context-click at the current mouse location.
actions.contextClick()
// Performs a double-click at the current mouse location.
actions.doubleClick()
// Performs a modifier key press.
actions.keyDown(Keys.ENTER)
// Moves the mouse to the middle of the element.
actions.moveToElement(destinationElement)
// Releases the depressed left mouse button at the current mouse location.
actions.release()
// Sends keys to the active element.
actions.sendKeys()
Java
C#
Python
Javascript
Kotlin
// Library to import
// import io.github.bonigarcia.wdm.WebDriverManager;
//Automatically downloads and configures the ChromeDriver binary
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
driver.quit();
//Automatically downloads and configures the ChromeDriver binary
new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());
IWebDriver driver = new ChromeDriver();
driver.Url = "https://www.testautomationstudio.com/demo/home/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
driver.Manage().Window.Maximize();
driver.Quit();
# Setup ChromeDriver automatically
service = Service(ChromeDriverManager().install())
# Initialize driver
driver = webdriver.Chrome(service=service)
driver.get("https://www.example.com")
driver.quit()
const { Builder } = require('selenium-webdriver');
async function runTest() {
let driver = await new Builder().forBrowser('chrome').build();
try {
await driver.get('https://www.google.com');
let title = await driver.getTitle();
console.log(title);
} finally {
await driver.quit();
}
}
runTest();
// Note: Selenium will automatically download and manage the correct ChromeDriver.
//Automatically downloads and configures the ChromeDriver binary
WebDriverManager.chromedriver().setup()
val driver: WebDriver = ChromeDriver()
driver["https://www.example.com"]
driver.quit()
Following are the command to configure the selenium Server.
java -jar selenium-server.jar standalone
java -jar selenium-server.jar hub
java -jar selenium-server.jar node
java -jar selenium-server.jar node --hub http://:4444
Example code to run test on Selenium Server
Java
C#
Python
Javascript
Kotlin
ChromeOptions options = new ChromeOptions();
//Add capability
//options.setCapability("");
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444"), options);
driver.get("https://www.testautomationstudio.com/demo/home/");
driver.quit();
ChromeOptions options = new ChromeOptions();
//Add Options
options.AddArgument("--headless");
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444"), options);
driver.Url = "https://www.testautomationstudio.com/demo/home/";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
driver.Manage().Window.Maximize();
driver.Quit();
options = ChromeOptions()
# Add capability
options.set_capability("name", "value")
driver = webdriver.Remote( command_executor ="http://localhost:4444", options= options)
driver.get("https://www.testautomationstudio.com/demo/home/")
driver.quit()
const { Builder } = require('selenium-webdriver');
async function runTest() {
let driver = await new Builder()
.usingServer('http://localhost:4444') // Grid URL
.forBrowser('chrome') // Browser to run
.build();
try {
await driver.get('https://www.google.com');
console.log(await driver.getTitle());
} finally {
await driver.quit();
}
}
runTest();
val gridUrl = URI.create("http://localhost:4444").toURL()
// As of Selenium 4, use browser-specific options classes
val options = ChromeOptions()
// options.addArguments("--headless")
// Instantiate the RemoteWebDriver
val driver: WebDriver = RemoteWebDriver(gridUrl, options)
driver.get("https://www.selenium.dev")
driver.quit()