Tag Archives: Python

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');

How to mouseover to reveal a hidden menu in Selenium

Java

In Java, create an Actions and use its moveToElement() method to mouse over the menu.
This method returns a self-reference, and you can call its perform() method to obviate the need to call build() first.
You can then click the revealed menu item in the normal way.

Actions actions = new Actions(driver);
actions.moveToElement(menuElement).perform();
By locator = By.id("menuItemID");
driver.click(locator);

Python

In Python, the class to use is ActionChains.

from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element(menu_element).perform()
element = driver.find_element_by_id("menuItemId")
driver.click(element)

C#

The process in C# is identical to Java, except for name conventions.

Actions actions = new Actions(driver);
actions.MoveToElement(menuElement).Perform();
By locator = By.Id("menuItemID");
driver.Click(locator);