Selenium Useful Commands
- Quality Engineering
Selenium Useful Commands
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
1 2 3 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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
1 2 3 |
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
1 2 3 |
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
1 2 3 |
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
1 |
driver.switchTo().frame(index); |
By Id or Name
1 2 |
driver.switchTo().frame("name"); driver.switchTo().frame("id of the element"); |
By WebElement
1 |
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
1 |
driver.switchTo().alert().dismiss(); |
To click on the ‘OK’ button on the alert
1 |
driver.switchTo().alert().accept(); |
To capture alert message
1 |
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-
1 2 |
import io.appium.java_client.TouchAction; import io.appium.java_client.touch.offset.PointOption; |
Then define variables-
1 2 |
public static AndroidDriver<MobileElement> driver; public static TouchAction action; |
Tap element using point of coordinates-
1 2 3 |
action = new TouchAction(Helper.driver); Thread.sleep(1000); action.tap(PointOption.point(x, y)).perform(); |
Single Tap
1 2 3 |
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
1 2 |
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy(0,y)"); |
Horizontal Scroll
1 2 |
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.
1 2 |
Helper.driver.findElementByAndroidUIAutomator( "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\"" + text + "\").instance(0))"); |
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s