Selenium 3 and click() command - For clicking a hyper link

click() command in Selenium 3 version will work same as it worked in Selenium 2 version.

In this post, I will demonstrate how to use click() command in Selenium 3 for clicking an hyper link.


Before using the click() command in Selenium 3, we have to first create a Java project and configure the project with Selenium 3 and chrome driver. Please go through the below link for the same:

  • Click here to configure the Java project with Selenium 3 and chrome driver
  • Also check whether you have the latest version of chrome browser in your machine. 

Once the above configuration is done, go through the below steps for executing click() command in Selenium 3 for clicking an hyper link.

1. Create a Java class file under 'src' and create a new class say 'Demo' as shown below:

2. Write the below four lines of code as shown in the screen-shot, in which the first line will set the path to chromedriver.exe file, the second line will open the chrome browser, the third line will open the web application in the browser and the fourth line will maximize the browser window:

3. Open the in the older version of Firefox Browser which supports and has FireBug and Firepath add-ons installed. If you don't have the desired version of Firefox Browser, go through the below post:

The firefox browser window having the opened in it, should be shown as below:

4. Right click on the 'compendiumdev' hyper link on the page and select 'Inspect in Firepath' option as shown below:

5. Do to the above action, xpath locator text for the above selected hyperlink will be auto-generated in the Firepath as shown below:

6. Copy the auto-generated xpath text and write the code which uses click() command of Selenium WebDriver for clicking the hyperlink as shown below:

7. Click on 'Save All' option to save the so far written code as shown below:

8. Right click on the newly created Java class and select to Run As > Java Application as shown below :

9. Observe that the 'compendium' link on the will be clicked by the above written selenium scripts and user will be taken the a different web page as a result as shown below:


click() command needs to be used in Selenium 3 for clicking any hyperlink on the web pages.

No comments: