Tag Archives: Python

How to maximize the window in Selenium

Java

In Java, the Webdriver’s manage() method returns an Options object, which has a window() method that returns a Window.
Window has a Maximize() method.

driver.manage().window().maximize();

Python

In Python, the Webdriver has a maximize_window() method.

driver.maximize_window()

C#

In C#, the Webdriver’s Manage() method returns an IOptions object, which has a Window() method that returns an IWindow.
IWindow has a Maximize() method.

driver.Manage().Window.Maximize();

How to select from a drop-down in Selenium

Use the Select class, which provides methods to select by visible text or value.

Java

Select select = new Select(driver.findElement(By.id("myID")));
select.deselectAll(); // If this is a multi-select
select.selectByVisibleText("My Text");
select.selectByValue("myValue");

Python

select = Select(driver.find_element_by_id("myId"))
select.deselect_all # If this is a multi-select
select.select_by_visible_text("My Text")
select.select_by_value("myValue")

C#

Select select = new Select(driver.FindElement(By.Id("myID")));
select.DeselectAll(); // If this is a multi-select
select.SelectByVisibleText("My Text");
select.SelectByValue("myValue");

How to refresh the page in Selenium

Java

In Java, the driver’s navigate() method returns a Navigation object, which has a refresh() method.

driver.navigate().refresh();

Python

In Python, the driver has a refresh() method.

driver.refresh

C#

In C#, the driver’s Navigate() method returns an INavigation object, which has a Refresh() method.

driver.Navigate().Refresh();

How to test if an element is present in Selenium

The findElement() method will throw an exception if it fails. To test for the presence of an element without having to catch an exception, use findElements() which returns an empty list if the element isn’t found.

Java

Boolean isPresent = driver.findElements(By.id("myID")).size() > 0;

Python

is_present = driver.find_elements_by_id("myID)

C#

bool IsPresent = driver.FindElements(By.Id("myID")).Count > 0;

How to wait until an element is present in Selenium

Use an explicit wait to only wait as long as necessary for an element to be present.

Java

WebElement element = (new WebDriverWait(driver, 10))
    .until(ExpectedConditions.presenceOfElementLocated(By.id("myID")));

Python


try:
    element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "myID")))
finally:
    driver.quit()

C#

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until<IWebElement>(d => d.FindElement(By.Id("myID")));

How to scroll an element into view in Selenium

There are two ways of doing this:

  • Using JavaScript and the scrollIntoView() function
  • Using Actions

Using JavaScript and the scrollIntoView() function

Java

WebElement element = driver.findElement(By.id("myID"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(500); 

Python

element = driver.find_element_by_id("myID")
driver.execute_script("arguments[0].scrollIntoView(true);", element)
time.sleep(0.5)

C#

WebElement element = driver.FindElement(By.Id("myID"));
((IJavascriptExecutor) driver).ExecuteScript("arguments[0].scrollIntoView(true);", element);
Thread.Sleep(500); 

Using Actions

Java

WebElement element = driver.findElement(By.id("myID"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();

Python

from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(driver.sl.find_element_by_id("myID")).perform()

C#

WebElement element = driver.FindElement(By.id("myID"));
Actions actions = new Actions(driver);
actions.MoveToElement(element);
actions.Perform();

How to wait for a page to load in Selenium

A good indication that the page has loaded is when the document object’s readyState property is “complete”.
You can use a custom wait to wait for this.

Java

Wait<WebDriver> wait = new WebDriverWait(driver, 30);
wait.until(new Function<WebDriver, Boolean>() {
    public Boolean apply(WebDriver driver) {
        return String
            .valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState"))
            .equals("complete");

Python

class document_is_ready(object):
    def __call__(self, driver)
        return driver.execute_script("return document.readyState").equals("complete"))

C#

IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").
                          Equals("complete"));

How to type the Enter/Return key in Selenium

Java

In Java, use the org.openqa.selenium.Keys enumeration.

import org.openqa.selenium.Keys
driver.findElement(By.id("myID")).sendKeys(Keys.RETURN);

Python

In Python, use the Keys enumeration from selenium.webdriver.common.keys.

from selenium.webdriver.common.keys import Keys
driver.find_element_by_id("myID").send_keys(Keys.RETURN)

C#

In C#, use the OpenQA.Selenium.Keys enumeration.

using OpenQA.Selenium.Keys
driver.FindElement(By.Id("myID")).SendKeys(Keys.Return);