selenium webdriver tips & tricks

Sunday, 01 February 2015 07:14

There is no method verifyTextPresent in Selenium WebDriver, so if you need to check text on the page, you can use the following methods:

public boolean verifyTextPresent(String text) {
    return driver.getPageSource().contains(text);


public boolean verifyTextPresent(String text) {
    return driver.findElement(By.tagName("body")).getText().contains(text);

Working with frames

To work with frames, you can use the following methods.

I recommend the following method:

WebElement myFrame = driver.findElement("iframe_canvas"));

You can switch to a frame by name:


As well as by index:


Working with windows

To work with windows, I recommend using the following method:

public void switchToWindow(int numberWindow) {
    String handle = driver.getWindowHandles().toArray()[numberWindow].toString();

Use the implicit waiting

I recommend always include implicit waiting, where they are not needed, just turn them off.

public void turnOnImplicitlyWait(int timeout) {
    driver.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);

public void turnOffImplicitlyWait () {
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

Using Drag and Drop

Drag and Drop is performed in that way:

Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(someElement)

Files uploading

If you have a file upload field <input type=file>, then implement a file download in the following way:


IntexSoft is in the Clutch Top 10 of Belarusian Web & Software Developers More