- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Advertisements
To Continue Learning Please Login
Login with Google