Clicking a Button with Selenium in Python

Modern web pages are fitted with so many functionalities. Buttons are one of the fundamental components of them. Selenium Python API bindings allow us to access the Selenium WebDrivers to click a button automatically. In this article, we will follow step by step approach to click on buttons.

Setting Up the Environment

We need to import selenium first to start our operation and then we will import a WebDriver that permits us to automate the web pages. We will use Chrome Webdriver to navigate through the chrome browser. The ‘time’ module needs to be imported as it will allow some time for the browser to load the page properly.

from selenium import webdriver
from selenium.webdriver.support.select import Select
import time 
driver = webdriver.Chrome(executable_path = r'G:/scraping_practice/chromedriver_win32/chromedriver.exe')
driver.get('https://www.imdb.com/search/title/')

The chrome driver executable path is selected. Now the driver is ready to navigate to the IMDb Advanced Title Search page:

Finding the Button

To click on a button first we need to find the button. We need to open the page in Chrome. Right-click on the button and select the Inspect Element from the context menu. The inspector window will open with the button’s HTML markup selected.

Then, we can find the button on the web page by using methods like:

  • find_element_by_class_name()
  • find_element_by_css_selector()
  • find_element_by_id()
  • find_element_by_xpath()
  • find_element_by_link_text()
  • find_element_by_id()
  • find_element_by_name()
  • find_element_by_tag_name()
  • find_element_by_partial_link_text()

Clicking a Radio Button Using “id”

We will find the “exclude titles I’ve seen” radio button from the “your ratings” option down the IMDb Advanced Title Search page.

Let’s right-click the button and then click to “inspect” element from the context menu. We can see from the Html that the <input> tag is controlling the radio button(see image:2). this tag has an attribute ‘id’. So we can find the radio button with the find_element_by_id() method. let’s check the code:

driver.find_element_by_id('my_ratings|exclude')

We can create a variable ‘button’ of type WebElement and then click on it using the click() method.

button = driver.find_element_by_id('my_ratings|exclude')
button.click()
time.sleep(3)

The “Exclude Titles I’ve seen” radio button will be selected when the code is executed. We used time.sleep(3) method to allow 3 seconds to load the next page properly.

Clicking the Search Button Using xpath

At the last part of the page, we can see a search button. how to click on it? Let’s inspect it

As we can see it’s a button tag and we can not use the class here to find the element as it is a common “primary” class. Yet we can find it with the “find_element_by_xpath” method to know the xpath, right-click on the <button> tag, go to copy, and then select the “Copy Xpath” option from the context menu. The xpath for the <button> tag will be copied. then make it a variable and just apply the click() method on it. The code will look like this:

search_button = driver.find_element_by_xpath('//*[@id="main"]/p[3]/button')
search_button.click()

When we run the code, we will be forwarded to another page after hitting the “Search” button.

Clicking the “Sign In” Button Using Link Text

At the top right corner of the “Advanced Title Search” page, we can see a “Sign In” option button. Let’s right-click on it and inspect.

Though a class name is available here in <div> tag, we can use an interesting method here called find_element_by_link_text(). With this method, the first element with the link text value matching the location will be returned. If any element matching the link text attribute is not found, a NoSuchElementException will be raised.

So here we just need to put the exact text as a parameter inside the find_element_by_link_text() method to find the element. Let’s try to click the “Sign In” button with this method.

sign_button = driver.find_element_by_link_text('Sign In')
sign_button.click()

As we check the code we will see, we will be transferred to the Sign In page. This find_element_by_link_text() method seems to be very easy to work with even for a novice.

How to Right-Click on a Button

Selenium module has another useful class called ActionChains that we can use to perform complex operations like mouse hover over, drag and drop, keypress, etc. We will have a detailed Finxter blog tutorial on ActionChains. Today we will use it to right-click on the “Sign In” button.

We need to import ActionChains class from selenium.webdriver. Then we will create an object of action chain class to right-click on it. This ActionChains class will take the driver as an argument.

from selenium.webdriver import ActionChains 
actions  = ActionChains(driver)

Now we need to inspect the WebElement for the “Sign In” button. As we have done it before, we can copy and paste it here.

sign_button = driver.find_element_by_link_text('Sign In')

Now for the right-click option, we will grab another method called context_click. This method in the ActionChains class performs a context-click (right-click) on an element. The perform() method will be required to execute the click as defined in the ActionChains class.

actions.context_click(sign_button).perform()

As we execute, the Chrome browser will load and automatically right-click on the “Sign In” button.

Now if we want to click on a button in the context menu, we can do it in the same way as we have done it for clicking a button.

How to Double-Click on a Button

To double click on a button or menu, we will follow the same procedure as we have followed for the right click on a button instruction. But here we will use double_click() method instead of context_click() . This double_click() method allows us to double-click an element. Just, for example, the code for double-clicking the “Sign In” button will be.

from selenium.webdriver import ActionChains 
actions  = ActionChains(driver)
sign_button = driver.find_element_by_link_text('Sign In')
actions.double_click(sign_button).perform()

To know more about various classes and methods of selenium package in Python, feel free to check out the docs. If you want to boost your Python skills, check out our free email academy with Python cheat sheets: