Home/Selenium Useful Commands

Selenium Useful Commands

Published On: 30 May 2019.By .
  • Quality Engineering

How to handle Autocomplete text box in Selenium WebDriver

Now a days, in most of the applications, we can see a ‘Auto Complete’ text boxes which will help users to quickly find the option from a pre-populated list of values based on the text that is entered by the user. We can handle autocomplete text box by two ways- by index or by text visible.

By index

driver.findElement(By.xpath("your searchBox")).sendKeys("your partial keyword"); Thread.sleep(3000);  //external wait
List <WebElement> listItems = driver.findElements(By.xpath("your list item locator")); listItems.get(0).click(); 
driver.findElement(By.xpath("your searchButton")).click()

By text visible

driver.findElement(By.xpath("your searchBox")).sendKeys("your partial keyword"); Thread.sleep(3000); 
WebDriverWait wait = new WebDriverWait(driver,30);
		wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("your list item locator")));
		
		List<WebElement> list = driver.findElements(By.xpath("your list item locator"));
		
		System.out.println("Auto Suggest List ::" + list.size());
		
		for(int i = 0 ;i< list.size();i++)
		{
			System.out.println(list.get(i).getText());
			
			if(list.get(i).getText().equals("your search word"))
			{
				list.get(i).click();
				break;
			}

How to select dropdown in Selenium WebDriver

In this we will see how to handle drop down in selenium webdriver. Firstly to do so we have to import package named org.openqa.selenium.support.ui.Select then we will find elements and then select element either by visible text, index or value.

Select by visible text

import the package org.openqa.selenium.support.ui.Select
Select dropdownItem = new Select(driver.findElement(By.name(“name of dropdown”)));
dropdownItem.selectByVisibleText(“your text”);

Select by index

import the package org.openqa.selenium.support.ui.Select
Select dropdownItem = new Select(driver.findElement(By.name(“name of dropdown”)));
dropdownItem.selectByIndex(index);

Select by value

Import the package org.openqa.selenium.support.ui.Select
Select dropdownItem = new Select(driver.findElement(By.name(“name of dropdown”)));
dropdownItem.selectByValue(“value”);

How to handle frames in Selenium WebDriver

IFrame is a web page which is embedded in another web page or an HTML document. It is basically used to insert content from other sources into the webpage. SwitchTo is used to handle frames in Selenium Webdriver.

By Index

driver.switchTo().frame(index);

By Id or Name

driver.switchTo().frame("name");
driver.switchTo().frame("id of the element");

By WebElement

driver.switchTo().frame(WebElement);

How to handle alerts in Selenium WebDriver

Alert is basically a small message box which is shown as notification to the user to give some kind of message to the user or to ask for any permission. It can also be used to show warnings to the user.

To click on the ‘Cancel’ button on the alert

driver.switchTo().alert().dismiss();

To click on the ‘OK’ button on the alert

driver.switchTo().alert().accept();

To capture alert message

driver.switchTo().alert().getText();

How to tap element in Selenium WebDriver in Mobile

In order to use Tap firstly we have to import two packages import io.appium.java_client.TouchAction and import io.appium.java_client.touch.offset.PointOption. We can single tap on element or we can tap on element using coordinates.

Firstly import the following packages-

import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.offset.PointOption;

Then define variables-

public static AndroidDriver<MobileElement> driver;
public static TouchAction action;

Tap element using point of coordinates-

action = new TouchAction(Helper.driver);
Thread.sleep(1000);
action.tap(PointOption.point(x, y)).perform();

Single Tap

TouchActions action = new TouchActions(driver);
action.singleTap(element);
action.perform();

How to Scroll using Selenium WebDriver

Scroll bar is basically used to move around the screen if the whole content is not visible on the screen. Two types of scroll is basically used- Vertical scroll and Horizontal scroll. Vertically scroll is basically used to move the scroll bar vertically and Horizontal scroll is used to move the scroll bar horizontally.

Vertical Scroll

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,y)");

Horizontal Scroll

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(x,0)");

How to Scroll using Selenium WebDriver in Mobile

UI Automator is used to scroll in Mobile. It will scroll the page until users find the text upto which we want to scroll.

Helper.driver.findElementByAndroidUIAutomator(
				"new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\"" + text + "\").instance(0))");

Related content

We Love Conversations

Say Hello
Go to Top