inapp /Independent Software Testing/Useful tips in Selenium WebDriver

Useful tips in Selenium WebDriver

May 3, 2015

6:20 am

The integration of WebDriver API has been a major change in Selenium in terms of browser automation. WebDriver has a well-designed object-oriented API that provides a simpler and more concise programming interface. Along with Selenium, the WebDrivers can make direct calls to the browsers by directly using the browser’s built in support for automation. The following figure shows Selenium Web server as an intermediary that interacts with the script (in Python / PHP) and the browser (Chrome / IE / Safari / Firefox).


Here are some useful tips on using Selenium WebDriver.

1. Open different browsers in web driver
a. Firefox Driver
WebDriver webDriver= new FirefoxDriver();
b. Chrome Driver
WebDriver webDriver= new ChromeDriver();
c. Internet Explorer Driver
WebDriver webDriver= new InternetExplorerDriver();
d. Safari Driver
WebDriver webDriver= new SafariDriver();

2. Open new Tab
webDriver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL +”t”);

3. Close newly opened tab
webDriver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL +”w”);

4. Wait for an element to be available in page
a. Method one

WebDriverWait wait = new WebDriverWait(driver, 30);

WebElement element =

b. Method two

List<WebElement> sElements = webDriver.findElements(By.xpath(xPath));

int sCount = 0;

while (sCount < sTime) {

if (sElements.size() > 0) {


} else {


sCount = sCount + 1;

sElements = webDriver.findElements(By.xpath(xPath));


5. Drag and Drop items

Actions builder = new Actions(driver);

Action dragAndDrop = builder.clickAndHold(someElement)





6. Execute Java Script statement

JavascriptExecutor jsx = (JavascriptExecutor) driver;

jsx.executeScript(“sample code”);

7. Enable elements that will be visible or enabled only on mouse over

${iid}= webDriver.findElement(“id123″)).getAttribute(attributevalue);
Execute Javascriptvar innerDiv = document.querySelector(‘DOM-
DOM-element can be div or input or li

8. Upload a file – We can do it in 2 methods

a. Method1 – If the upload button is defined as DOM element – ‘input’ use following method
WebElement w = getWebDriver().findElement(By.xpath(element-xpath));
b. Method2 – If the upload button is not defined as – ‘input’

String filePath = “path-of-file-for-upload”;
JavascriptExecutor jsx = (JavascriptExecutor) driver;
jsx.executeScript(“document.getElementById(‘fileName’).value=’” + filePath + “‘;”);

9. Switch between frames

WebElement iframe = getWebDriver().findElement(By.xpath(“//iframe xpath”));


10. Switch back to default content from the frame

After performing actions in the iframe that was switched we might switch back to default content to
perform further activities


11. Switch to new window

for (String winHandle : webDriver.getWindowHandles()) {



12. Switch tab

String oldTab = webDriver.getWindowHandle();

ArrayList<String> newTab = new ArrayList<String>(



13. Wait for page load

webDriver.manage().timeouts().pageLoadTimeout(100, TimeUnit.SECONDS);

14. Scroll window to bottom, to view elements

webDriver = getCurrentWebDriver();

JavascriptExecutor jsx = (JavascriptExecutor) webDriver;

jsx.executeScript(“window.scrollTo(0, document.body.scrollHeight);”);

15. Scroll window to specific height -scrollHeight is the value passed to function

webDriver = getCurrentWebDriver();

JavascriptExecutor jsx = (JavascriptExecutor) webDriver;

jsx.executeScript(“window.scrollTo(0, ” + scrollHeight + “);”);