Selenium Click Button and Link

Well you can do a lot of things in Selenium. Hereby we are going to see how to perform clicks in Selenium.  Selenium Click Button and maybe a link.  It is a fairly easy chapter and to be honest, a lot of you already know how to do it. But still this is for those who are struggling with even the most basic of things. And like Cervantes once said,

Rome wasn’t built in a day.

The basics help you get there. So let’s tend to the basics before becoming a pro at it.

click the like button meme

The first thing you gotta do is identify the element you wish to click. It could be a button or could be a link or could be right about anything.

We will see a button first and then move on to the link part.

Selenium Click Button

So assuming that you have your website open we will try to identify the thing that needs to be clicked. I am going to take the example of Google once again and try to click on the I’m Feeling Lucky button.

Step 1: The first thing that we gotta do is open the link of course. You could easily do so by using the get method:

driver.get("http://www.google.com");

The next big thing is the identification of the button. We will bring in to use the knowledge that we had acquired about XPaths earlier, and apply it here.

Step 2:  So clicking open the Inspector or Developer Tools by pressing F12:

inspector box

and then pressing Ctrl + F for checking if what XPath we are going to write identifies the Google Search button or not:

textbox for entering xpath

 

Step 3: Type the following in the XPath box to check if it highlights the I’m Feeling Lucky button or not:

//input[@value="I'm Feeling Lucky"]

Now checking:

xpath to highlight the button I'm Feeling Lucky on Google

 

It does. So we have our XPath now. Let’s use it in our code:

Step 4: Let’s find the element first by using driver.findElement like this:

 driver.findElement(By.xpath("//input[@name='btnI']"));

Now since we want to click on it, we are going to simply add the click() method at the end of it like this:

 driver.findElement(By.xpath("//input[@name='btnI']")).click();

So our whole code would look something like this:

code for clicking on button

 

Time to run this baby.

Step 5: Execute the code to see the results:

google doodle page result

It has worked! Yay!

Apparently Google was running some Doodles on its page, and it has redirected us to that after clicking on the ‘I’m Feeling Lucky’ button.

Selenium Click Link

Working on a similar logic of Selenium Click Button is Selenium Link Click. All you have to do here too is identify the element first and the follow it up by .click().

Let’s check an example where we will click the Images button on Google.

Step 1: Open Google.com first using driver.get:

The first thing that we gotta do is open the link of course. You could easily do so by using the get method:

driver.get("http://www.google.com");

images link of google

Then you gotta identify the images link using XPath.

Step 2: Open the Developer Tools again or press F12 and then press Ctrl + F to start typing the xpath in the textbox area.

google images link's xpath

Step 3: Writing the following has illuminated the Images so we are spot on here. This is it:

//*[contains(text(), 'Images')]

Step 4: Now time to use the driver.findElement method to identify it and append click() method at the end like this:

driver.findElement(By.xpath("//*[contains(text(), 'Images')]")).click();

The whole could would look something like this:

code for clicking images link on google

Step 5: Execute it.

The google Images version of Google reflects on the page which means successful execution:

google images site

Alright so the above logic can be applied to literally anything you wish to click on. Just find that element first, identify it and then add the click() method at the end.

 

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

Leave a Reply