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.
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:
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:
and then pressing Ctrl + F for checking if what XPath we are going to write identifies the Google Search button or not:
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"]
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:
Now since we want to click on it, we are going to simply add the click() method at the end of it like this:
So our whole code would look something like this:
Time to run this baby.
Step 5: Execute the code to see the results:
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:
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.
Step 3: Writing the following has illuminated the Images so we are spot on here. This is it:
Step 4: Now time to use the driver.findElement method to identify it and append click() method at the end like this:
The whole could would look something like this:
Step 5: Execute it.
The google Images version of Google reflects on the page which means successful execution:
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.