DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Related

  • Google Cloud Pub/Sub: Messaging With Spring Boot 2.5
  • How Spring and Hibernate Simplify Web and Database Management
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Enabling Behavior-Driven Service Discovery: A Lightweight Approach to Augment Java Factory Design Pattern

Trending

  • The Shift of DevOps From Automation to Intelligence
  • Integrating Selenium With Amazon S3 for Test Artifact Management
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Building an IoT Framework: Essential Components for Success
  1. DZone
  2. Coding
  3. Frameworks
  4. Selenium With Java: Google Search

Selenium With Java: Google Search

If you're looking to get started with Selenium with Java, this is the post for you. Let's take a look at browser automation with this example.

By 
Michael Good user avatar
Michael Good
·
Feb. 22, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
36.5K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial, we will be exploring the basics of how to use Selenium with Java. We will use Selenium to open Google, search, and click a URL.

The code is available on GitHub.

What Is Selenium?

Selenium automates web browsers. That’s really it.

Selenium enables us to emulate user interaction with a web page. There are two Selenium products we can use: Selenium WebDriver and Selenium IDE. We will be using WebDriver.

What is WebDriver? WebDriver is an official W3C Specification, and in essence, it is a way of interacting with a web browser. Previously, with Selenium RC, Selenium would operate with the browser by injecting JavaScript to interact with elements. With the adoption of the WebDriver specification, companies like Google, Mozilla, and Microsoft release their browser with the ability to be controlled by a hook, that Selenium can tap into. This hook enables Selenium to interact with the web browser in the same way that humans do.

We will be using Google Chrome — therefore, it is required that we download the chromedriver.

After downloading the driver, we need to execute the file.

On Macs, we can simply do this, for instance:

./chromedriver


pom.xml

I use Spring Tool Suite and created a new Spring Starter project, which wasn’t necessary, but I tend to like Spring. So Selenium is managed by the Spring Boot Starter Parent actually. The version is 2.53.1.

<!-- typical pom beginning-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
<!-- typical pom ending-->


Open Chrome and Search

For this step, we will be establishing the connection to the chromedriver, opening the browser, and searching for “Selenium”.

The port we target for our localhost is 9515 because the chromedriver runs on the local server’s port 9515.

RemoteWebDriver implements WebDriver and WebDriver’s goal is to supply an object-oriented API that provides support for modern advanced web-app testing problems. So, we can tell based on these facts that RemoteWebDriver is the implementation that allows the use of a remote browser. The benefits include separation of where the tests run from where the browser is and the ability to test with browsers not available on the current OS. The cons include the fact that we need an external servlet container to be running and there may be latency if an exception is thrown.

// create a Chrome Web Driver
URL local = new URL("http://localhost:9515");
WebDriver driver = new RemoteWebDriver(local, DesiredCapabilities.chrome());
// open the browser and go to open google.com
driver.get("https://www.google.com"); 

driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
driver.findElement(By.name("btnK")).click();
driver.manage().window().maximize();


Get Pages and Click

WebDriver gives us the methods findElement and findElements method to locate element(s) on a web page. These methods accept a By object as a parameter. By has methods to locate elements within a document with the help of a locator value. Selenium has documented their API well.

Once we understand how Selenium is used to identify elements, it is easy to read any of the the driver.findElements(By…) methods. But we need to know how to write them too. Using a browser, like Chrome, we can right-click (or the equivalent) to Inspect an element to get its HTML/CSS information. Also, we can “View Source” to get more complete information as well.

To demonstrate how to scroll on a web page, we perform jse.executeScript(“window.scrollBy(0,250)”, “”).

Like the name suggests, JavaScriptExecutor executes JavaScript. JavaScriptExecutor is an interface provided through Selenium WebDriver. It provides two methods, “executescript” and “executeAsyncScript”, to run JavaScript on the selected window or current page.

With the code below, it may be possible to create a more comprehensive bot to search Google and click URLs for several pages.

// get the number of pages
   int size = driver.findElements(By.cssSelector("[valign='top'] > td")).size();
   for(int j = 1 ; j < size ; j++) {
       if (j > 1) {// we don't need to navigate to the first page
           driver.findElement(By.cssSelector("[aria-label='Page " + j + "']")).click(); // navigate to page number j
       }

       String pagesearch = driver.getCurrentUrl();

       List<WebElement> findElements = driver.findElements(By.xpath("//*[@id='rso']//h3/a"));
       System.out.println(findElements.size());

       for(int i=0;i<findElements.size();i++){
           findElements= driver.findElements(By.xpath("//*[@id='rso']//h3/a"));                
           findElements.get(i).click(); 

           driver.navigate().to(pagesearch);
           JavascriptExecutor jse = (JavascriptExecutor) driver;
           //Scroll vertically downward by 250 pixels
           jse.executeScript("window.scrollBy(0,250)", "");
       }
   }


Conclusion

This was a basic introduction to Selenium with Java. As we discovered, in Selenium Webdriver, locators like XPath, CSS, etc. are used to identify and perform operations on a web page. It is also possible to execute arbitrary JavaScript.

The complete code can be found on GitHub.

Google (verb) Google Search Spring Framework Java (programming language)

Published at DZone with permission of Michael Good, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Google Cloud Pub/Sub: Messaging With Spring Boot 2.5
  • How Spring and Hibernate Simplify Web and Database Management
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Enabling Behavior-Driven Service Discovery: A Lightweight Approach to Augment Java Factory Design Pattern

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: