Click Button in Selenium with Python



We can click a button with Selenium Webdriver in Python using the click() method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, XPath, tagname, or CSS selector. Then we have to apply the click() method on it.

Common Approaches

Two common methods for clicking a Selenium button in Python are using locators such as Xpath and with JavaScript Executor.

  • Using X_path Locator: The attribute which is used to locate the button with the text on the webpage.

  • Using the 'execute_script()' Method: This approach uses JavaScript to click the button.

Locating the Button Using X_path

The Xpath(XML Path Language) is used to locate elements in a web page. We can this method when the elements do not have easily accessible like IDs or class names.

l = driver.find_element_by_xpath("//button[text()='Check it Now']")

This method provides the full path to the element from the root of the document. By using this X_path we can locate elements based on various attributes(text, class).

Opening Browser and URL

We have to create a WebDriver object that makes us to open a web browser and the URL of the web page. In the below code 'get()' function is used to open selenium URL ('https://www.Example.com/') in chrome browser.

# creating an object of  chrome webdriver
driver = webdriver.Chrome(executable_path = r'./chromedriver')
# To open selenium URL in chrome browser
driver.get('https://www.Example.com')

Code Implementation with click method

In the below example code driver = webdriver.Chrome() function initializes the browser using Selenium and provides the path to the chromedriver executable. Once the button is found, the click() method performs a click action on the button.

from selenium import webdriver

# set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")

# implicit wait
driver.implicitly_wait(0.5)
#maximize browser
driver.maximize_window()

# launch URL
driver.get("https://www.tutorialspoint.com/index.htm")
#identify element
l =driver.find_element_by_xpath("//button[text()='Check it Now']")

# perform click
l.click()
print("Page title is: ")
print(driver.title)

# close browser
driver.quit()

Using 'execute_script()' Method

Selenium can execute commands like JavaScript with the help of execute_script() and arguments[0].click() methods and the element locator (X_path_) are passed as a parameter to this method.

driver.execute_script("arguments[0].click();", l);

Code Implementation with execute method

In the below example code driver.execute_script() function uses JavaScript to click the button, where the expression "arguments[0].click();" clicks on the the passed element.

from selenium import webdriver

# set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")

# implicit wait
driver.implicitly_wait(0.5)
#maximize browser
driver.maximize_window()

# launch URL
driver.get("https://www.tutorialspoint.com/index.htm")
# identify element
l =driver.find_element_by_xpath("//button[text()='Check it Now']")

# perform click with execute_script
driver.execute_script("arguments[0].click();", l);
print("Page title is: ")
print(driver.title)

# close browser
driver.quit()

Output


Updated on: 2024-09-10T16:31:04+05:30

53K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements