Tag Archives: Selenium

Selenium Howtos

How to take a screenshot
How to get the HTML source of an element
How to use Selenium headless
How to type the Enter/Return key
How to wait for a page to load
How to find an element that contains specific text
How to scroll an element into view
How to wait until an element is present
How to test if an element is present
How to refresh the page
How to select from a drop-down
How to get the current URL
How to maximize the window
How to mouseover to reveal a hidden menu
How to clear the text from a TEXTAREA
How to open a new tab
How to check if a checkbox is checked
How to click the OK button in an Alert
How to check if an element is visible
How to get an attribute of an element
How to get the current contents of a form text element
How to execute JavaScript
How to get the style of an element

How to get the style of an element in Selenium

Java

In Java, the WebElement has a getCssValue() method.

String display = driver.findElement(By.id("myID")).getCssValue("display");

Python

In Python, the WebElement has a value_of_css_property() method.

display = driver.find_element_by_id("MyID")).value_of_css_property("display")

C#

In C#, the IWebElement has a GetCSSValue() method.

string display = driver.FindElement(By.Id("myID")).GetCssValue("display");

How to execute JavaScript in Selenium

Java

In Java, the Webdriver can be cast to a JavaScriptExecutor, which has an executeScript() method, which returns an Object.

String title = ((JavascriptExecutor) driver).executeScript("return document.title;").toString();

Python

In Python, the Webdriver has an execute_script() method.

title = driver.execute_script("return document.title;")

C#

In C#, the Webdriver can be cast to an IJavaScriptExecutor, which has an ExecuteScript() method, which returns an Object.

String title = ((IJavaScriptExecutor) driver).ExecuteScript("return document.title;").ToString();

How to get the current contents of a form text element in Selenium

Use the value attribute for an <input> element, and the text property for a <textarea>.

Java

String value = driver.findElement(By.tagName("input")).getAttribute("value");
String text = driver.findElement(By.tagName("textarea")).getText()

Python

value = driver.find_element_by_tag_name("input")).get_attribute("value");
text = driver.find_element_by_tag_name("textarea")).text

C#

string value = driver.FindElement(By.TagName("input")).GetAttribute("value");
string text = driver.FindElement(By.TagName("textarea")).Text;

How to find out if a checkbox is checked in Selenium

You can’t reliably use the checked attribute for this, as this only indicates whether that attribute has been set in code (HTML or JavaScript), not whether the user has checked the checkbox. Instead, you need to use the isSelected() method.

Java

boolean isChecked = driver.findElement(By.id("myInput")).isSelected();

Python

is_checked = driver.find_element_by_id("myInput").is_selected()

C#

bool isChecked = driver.FindElement(By.Id("myInput")).Selected;

How to open a new tab in Selenium

The keystrokes needed to open a new tab are platform-specific. In Windows, they are Control + T.
You can either:

  • Open a new, empty tab by sending these keystrokes to the body of the document, or
  • Open a link in a new tab by sending the keystrokes to the link

Opening a new, empty tab

Java

WebElement body = driver.findElement(By.tagName("body"));
body.sendKeys(Keys.chord(Keys.CONTROL,"t"));

Python

body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')

C#

IWebElement body = driver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + 't');

Opening a link in a new tab

Java

WebElement link = driver.findElement(By.linkText("My link text"));
link.sendKeys(Keys.chord(Keys.CONTROL,"t"));

Python

link = driver.find_element_by_link_text("My link text")
link.send_keys(Keys.CONTROL + 't')

C#

IWebElement link = driver.FindElement(By.LinkText("My link text"));
link.SendKeys(Keys.Control + 't');