- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to click button Selenium 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.
Then we have to apply the click method on it. A button in html code is represented by button tagname. The click operation can also be performed with the help of the JavaScript Executor.
Selenium can execute JavaScript command with the help of execute_script method and the JavaScript command - arguments[0].click() and the webelement locator are passed as a parameter to this method
Syntax
l=driver.find_element_by_id("btn"); l.click(); //with JavaScript Executor driver.execute_script("arguments[0].click();", l);
Let us try to click the button CHECK IT NOW on the page −
Example
Code Implementation with method click
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()
Code Implementation with JavaScript Executor
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
- Related Articles
- Click a href button with Selenium and Python?
- Using Selenium in Python to click/select a radio button.
- How to click on a button with Javascript executor in Selenium with python?
- How to click on image in selenium webdriver Python?
- How to click on sign up button using Java in Selenium I am able to open page but not able to click?
- How to click on a link in Selenium with python?
- How to use a click() method in Selenium with python?
- How to click the 'Ok' button inside an alert window with a Selenium command?
- How to click on a link using Selenium webdriver in Python.
- How to create right click using selenium?
- How to perform double click on an element in Selenium with python?
- How to perform right click on an element in Selenium with python?
- How to handle a Button click event in tkinter?
- How to perform right click using Selenium ChromeDriver?
- How to click on a link in Selenium?

Advertisements