Skip to main content

Top 5 Selenium Webdriver Coding Tips to Improve Quality

SeleniumWebdriver is a powerful automation tool for testing web applications. It’s an open source library bundled with a rich set of features. Using it for testing a website is just like simulating the actions of a real user.

All these abilities together make it a perfect tool for automation. But you need proper guidance to get the best out of it. In this blog post, we’ve assembled some of the best and Top 5 Selenium Webdriver coding tips for software testers.

Tip-1: Best Method To Create Webdriver Instance.

It’s one of the most common Selenium Webdriver coding tips which you can’t avoid to use.

1.1- Use the <Factory> design pattern to create objects based on browser type.

1.2- Extend the below code or use it as is in your projects.

public class DriverFactory {

private WebDriver driver = null;

    public static WebDriver getBrowser(String browserType) {
        if (driver == null) {
            if (browserType.equals("Firefox"))

                driver = new FirefoxDriver();

            } else if (browserType.equals("Chrome"))

                driver = new ChromeDriver();

            } else if (browserType.equals("IE"))

                driver = new InternetExplorerDriver();

        return driver;

Tip-2: Simple Method To Check If An Element Exists.

2.1- You should use <findElements> instead of <findElement>.

2.2- <findElements> returns an empty list when it doesn’t find any matching elements.

2.3- You can use the code given below to check for the presence of an element.

Boolean isItemPresent = driver.findElements(By.testLocator).size() > 0

Tip-3: Avoid Exception While Checking An Element Exists.

3.1- The code in the previous tip may lead to <NoSuchElementException>. Also, it could be a bit slower in some cases.

3.2- Webdriver has a Fluent wait feature. We can use it to check the element.

3.3- It gives the ability to ignore any exception and allows to test the <WebElement>. 

Let’s see how to achieve what’s said here.

/* 1- Test if an element exists on the page.
2- Ignore the no element found exception.
By element = By.xpath(".//*[@id='demo']/p");
Wait < WebDriver > wait = new FluentWait < > (driver)
.withTimeout(60, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)

Tip-4: Wait For A Page With JavaScript(JS) To Load.

4.1- It’s quite common working with pages bloated with JavaScript. The challenge is how to make sure the page has finished loading.

4.2- Here, we are giving a solution that works. This code will help you setting

Latest Updated Selenium Topics 

wait.until(new Predicate < WebDriver > () {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");

Tip-5: Take A Screenshot Using Webdriver.

5.1- It’s easy to capture a screenshot of any error using Webdriver.

5.2- Use the below Webdriver code. It saves the screenshot to the current project directory.

WebDriver driver = new FirefoxDriver();
// Store the screenshot in current project dir.
String screenShot = System.getProperty("user.dir") + "\\screenShot.png";
// Call Webdriver to click the screenshot.
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
// Save the screenshot.
FileUtils.copyFile(scrFile, new File(screenShot));


I wish the above post would have been able to hit the right chord with you. I’ve tried hard to touch upon all basic, advanced and premium Selenium Webdriver coding tips.

I believe these quickies would not only solve your problems but help in upgrading your Selenium Webdriver coding skills.

So, help me reach this post further to benefit every other individual who’s striving to learn automation. Don’t just leave without a like, share, or comment.

Selenium Tutorials for Beginners


Popular posts from this blog

Top 50 Tricky Selenium Interview Questions

Previous Post: Good News for Selenium Testers 
1. What are the annotations used in TestNG? @Test, @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeClass, @AfterClass, @BeforeMethod, @AfterMethod 2. How do you read data from excel? FileInputStream fis = new FileInputStream (“path of excel file”); Workbook wb = WorkbookFactory.create (fis); Sheet s = wb.getSheet (“sheetName”) String value = s.getRow (rowNum).getCell (cellNum).getStringCellValue (); 3. What is the use of xpath? It is used to find the WebElement in web page. It is very useful to identify the dynamic web elements. 4. What are different types of locators? There are 8 types of locators and all are the static methods of the By class. () ()By.tagName ()By.className ()By.linkText ()

Complete Selenium Topics to be a Professional Selenium Tester

Complete Topics in Selenium to be a Professional Tester Free PDF Below

•        What is automation testing? •        When Automation Testing is needed? •        When Automation Testing is not needed? •        What is the use of automation testing? •        Different Automation Tools available in the Market. •        What is the difference between Selenium and QTP?
•        What is Selenium? •        Advantages of Selenium
•        IDE Overview •        RC Overview •        Selenium-Grid •        Selenium Web Driver/Selenium 3.0
•        Installing IDE •        IDE icons •        Recording your first test with IDE •        IDE Context Menu •        Assert •        Verify •        Adding IDE comments •        Synchronization commands •        Working on pages with AJAX •        Storing elements •        Creating test suites •        Tools  to identify elements/objects •        …

Top 10 Websites every Software Tester must Bookmark in 2018

For all the software testers, test automation experts and QA enthusiasts out there, we have compiled a list of 10 websites that you should bookmark in 2017. No matter, whether you want to solve a bug, get latest updates, find answers to the interview questions or anything else, this list of 10 websites will cover everything about QA and software testing. 1. Software Testing Help is a popular website focusing on Software Testing and Quality Assurance topics. With 1 million+ monthly views, the website caters to a wide range of testing professionals who are looking updates related to software testing tutorials, methodologies, manual testing, automation testing, testing tools, interview questions, web testing, testing templates, quality assurance, testing certifications, books, career guidance, job openings, latest testing trends, news, and much more. 2. Software Test Professionals (STP) is an online community providing software…