Useful tips in Selenium WebDriver

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).

Script-Selenium-Browser

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 =
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(‘xpath-of-element’)));

b. Method two

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

int sCount = 0;

while (sCount < sTime) {

if (sElements.size() > 0) {

break;

} else {

Thread.sleep(1000);

sCount = sCount + 1;

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

}

5. Drag and Drop items

Actions builder = new Actions(driver);

Action dragAndDrop = builder.clickAndHold(someElement)

.moveToElement(otherElement)

.release(otherElement)

.build();

dragAndDrop.perform();

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(By.id(“id123″)).getAttribute(attributevalue);
Execute Javascriptvar innerDiv = document.querySelector(‘DOM-
element#${iid}’);innerDiv.style.display=”block”;
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));
w.sendKeys(path-of-file);
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”));

getWebDriver().switchTo().frame(iframe);

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

getWebDriver().switchTo().defaultContent();

11. Switch to new window

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

webDriver.switchTo().window(winHandle);

}

12. Switch tab

String oldTab = webDriver.getWindowHandle();

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

webDriver.getWindowHandles());

webDriver.switchTo().window(newTab.get(0));

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 + “);”);



Leave a Reply

2 × 4 =