TechBeamersTechBeamers
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
TechBeamersTechBeamers
Search
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
Follow US
© TechBeamers. All Rights Reserved.
Automation TricksSelenium Tutorial

Selenium Webdriver Howtos (10) for 2025

Last updated: Feb 25, 2025 5:34 am
Meenakshi Agarwal
By
Meenakshi Agarwal
Meenakshi Agarwal Avatar
ByMeenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
Follow:
No Comments
3 months ago
Share
8 Min Read
SHARE

This tutorial discusses the ten essential Selenium Webdriver howtos that address many real-time issues with Selenium automation. Every how-to has a piece of code attached to demonstrate its usefulness. You can utilize the sample code right away in your projects.

Contents
Getting Started with Selenium Webdriver How-tosHow to use Selenium Webdriver to click Google searchWhat to do when the click doesn’t work in Selenium Webdriver?Provide code to simulate mouseover in Selenium WebdriverHow to open a URL in the new tab with Selenium WebDriverSelenium Webdriver code to extract data from the PDF filesHow to get elements by XPath using JavaScript in SeleniumHow to select the drop-down option in Selenium WebdriverCode to select all the drop-down values using Selenium WebDriver.Code to get all the children of an element using Webdriver.Provide code to get the visible text of a page.Summary: Selenium Webdriver HowTos

We must tell you that the tips here will provide a partial solution that you can reuse in your programs. So we assume you know the basics of Selenium Webdriver.

10 Most Common Selenium Webdriver Howtos

Getting Started with Selenium Webdriver How-tos

Let’s open up this tutorial with a solution to one of the most common use cases – How to automate web tables in a web application.

It is about handling the web tables where we will show how to query a table to fetch data from its cells.

//Create the Firefox driver instance.
WebDriver ff = new FirefoxDriver();

//First, find the table on the web page.
WebElement table = ff.findElement(By.id(searchResultsGrid));

//Second, fetch all the row elements of the table.
List<WebElement> rowSet = table.findElements(By.tagName("tr"));

//Third, loop through the rows and get the data from their cells.
for (WebElement row : rowSet) {
 List<WebElement> cells = row.findElements(By.xpath("./*"));
 for (WebElement cell : cells) {
 //Continue checking.
 }
}

Must read – 100+ Selenium interview questions to ramp up preparation

The above code was just a demo before we shared the rest of the useful Selenium tips. Go through the list below to see other pieces of Selenium Webdriver howtos.

How to use Selenium Webdriver to click Google search

Since Google optimizes its CSS and JS scripts, you can’t rely on using them as locators. Moreover, it adds the search links dynamically on the result page. So you use a smart wait for the page to show all the results.

/*
 # Selenium Webdriver to click Google search and wait for the results.
 # ===================================================================
 */
public static void main(String[] args) {

    WebDriver ff = new FirefoxDriver();
    ff.get("http://www.google.com");
    WebElement search = ff.findElement(By.name("q"));
    search.sendKeys("Hello world!\n");
    search.submit();

    // Wait until Google finishes displaying the results.
    WebElement waitForResult = (new WebDriverWait(ff, 10))
              .until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats")));

    List<WebElement> searches = ff.findElements(By.xpath("//*[@id='rso']//h3/a"));

    // Print all the links appeared in the search.
    for (WebElement lnk : searches)
    {
        System.out.println(lnk.getAttribute("href"));
    }
}

What to do when the click doesn’t work in Selenium Webdriver?

Firstly, you should check that the element you click exists on the target page. Then, you can try using the below code.

ff.findElement(By.name("submit")).sendKeys(Keys.Return);
or
ff.findElement(By.name("submit")).sendKeys(Keys.Enter);

Provide code to simulate mouseover in Selenium Webdriver

It’s not realistic to achieve <mouse hover> in Selenium Webdriver. We can sequence all the actions in one go. It will give the same effect as mouseover would have.

Here is the three-line code you can use.

Actions action = new Actions(ff);
WebElement seq = ff.findElement(By.xpath("html/body/div[10]/ul/li[5]/a"));
action.moveToElement(seq).moveToElement(ff.findElement(By.xpath("/paste-expression"))).click().build().perform();

How to open a URL in the new tab with Selenium WebDriver

Here is the Selenium Webdriver code snippet that will open a new tab.

It’ll open the page in a new tab.

String newtab = Keys.chord(Keys.CONTROL,Keys.RETURN); 
ff.findElement(By.linkText("URL")).sendKeys(newtab);

If you wish to move to the new tab, use the below code.

ArrayList<String> tablist = new ArrayList<String> (ff.getWindowHandles());
ff.switchTo().window(tablist.get(0));

These Selenium Webdriver Howtos are worth experimenting with, check out a few more below.

Selenium Webdriver code to extract data from the PDF files

Probably, you’ve heard many times that you need to check a page that loads the PDF.

Here is the code to verify the online PDF file. But you first need to download the Apache PDF library.

@Test
public void checkPDFpage() throws Exception {
    // Test page which loads pdf document.
    ff.get("http ... sample page loading pdf");
 
    URL nav = new URL(ff.getCurrentUrl());
    BufferedInputStream page = new BufferedInputStream(nav.openStream());
 
    PDDocument pdfdoc = null;
    try{
        pdfdoc = PDDocument.load(page);
        String result = new PDFTextStripper().getText(pdfdoc);
        System.out.println(result);
    }finally{
 
        if( pdfdoc != null )
        {
            pdfdoc.close();
        }//end of if
    }//finally
}//main

How to get elements by XPath using JavaScript in Selenium

Here is an easy way to retrieve the XPath using JS. Use the JS <document.evaluate> method to fetch the XPath.

function getElementByXpath(path) {
  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

Next, you may have to call the above JavaScript function from the Webdriver code. So use the <JavascriptExecutor> as given below.

WebDriver ff = new FirefoxDriver();
if (ff instanceof JavascriptExecutor) {
    ((JavascriptExecutor) ff).executeScript("getElementByXpath('//xpath')");
}

How is it going with our special list of Selenium Webdriver Howtos? We hope you are enjoying writing so much code.

How to select the drop-down option in Selenium Webdriver

Use the following Webdriver code. It’ll help in choosing the options based on the label.

Select sel = new Select(ff.findElement(By.xpath("//path_to_drop_down")));
sel.deselectAll();
sel.selectByVisibleText("myvalue");

Also, you can add the below code to get the first selected value.

WebElement selection = sel.getFirstSelectedOption();

Code to select all the drop-down values using Selenium WebDriver.

Here is the code to select all values from the drop-down.

WebElement sel = ff.findElement(By.id("month"));
List<WebElement> items = sel.findElements(By.tagName("option"));

for (WebElement item : items) {
       item.click();
}

Code to get all the children of an element using Webdriver.

You can query all the children of an element in Selenium Webdriver by using both the XPath and the CSS.

List<WebElement> nodes = targetElement.findElements(By.xpath(".//*"));

Or

List<WebElement> nodes = targetElement.findElements(By.cssSelector("*"));

Finally, we approached the last mile and the last of the 10 Selenium Webdriver Howtos.

Provide code to get the visible text of a page.

You need to run through the following steps to get the visible text.

  • Call <By.tagName(“body”)> to select the top element in the DOM.
  • Next, use the <getText()> methods on the same element. It will return the desired visible text.

Also Read: Top 20 Selenium Webdriver Coding Tips for Beginners

Summary: Selenium Webdriver HowTos

You might have observed that merely reading Selenium Webdriver tutorials won’t make you an automation geek. Several other technical things matter and you should know them as automation testers.

Most of the tutorials fail to address such common problems. Hence, we did trials, researched, and rinsed the above ten problems many automation testers face.

Lastly, our site needs your support to remain free. Share this post on social media and subscribe to our YouTube channel.

Keep Learning,
TechBeamers

Related

TAGGED:Advanced Selenium TutorialWebdriver Interview
Share This Article
Flipboard Copy Link
Subscribe
Notify of
guest

guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments

List of Topics

Stay Connected

FacebookLike
XFollow
YoutubeSubscribe
LinkedInFollow

Subscribe to Blog via Email

Enter your email address to subscribe to latest knowledge sharing updates.

Join 1,011 other subscribers

Continue Reading

  • How to Inspect Element in Safari, Android, and iPhoneFeb 11
  • Top 20 Playwright Interview QuestionsFeb 15
  • Playwright Python Quick Start GuideFeb 15
  • Playwright vs Selenium: Full ComparisonFeb 15
  • What is Playwright Testing?Feb 15
  • Essential Selenium Webdriver Skills for Test AutomationJun 2
  • Selenium Webdriver Coding Tips for 2025Jun 10
  • 4 Must-have Selenium IDE Add-ons for FirefoxJun 21
  • Selenium IDE Tips and TricksJun 30
  • Is Playwright Faster Than Cypress?Feb 20
View all →

RELATED TUTORIALS

Unable to install TestNG in Eclipse

Unable to install TestNG in Eclipse – What to do?

By Meenakshi Agarwal
11 months ago
How to add testng in eclipse

How to Install TestNG in Eclipse IDE

By Harsh S.
3 weeks ago
Selenium Webdriver Commands Essential for Test Automation

Learn Basic Selenium Webdriver Commands

By Harsh S.
3 months ago
Java Coding Questions for Software Testers

15 Java Coding Questions for Testers

By Meenakshi Agarwal
10 months ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
wpDiscuz