- Trending Categories
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
Click a href button with Selenium and Python?
We can click a link/button with its href link in Selenium webdriver. This can be achieved by multiple ways. We can use find_element_by_link_text() and find_element_by_partial_link_text() methods to perform this task.
The find_element_by_link_text() method is used to identify an element with the text within the anchor tag as specified in the method parameter . If there is no matching text, NoSuchElementException is thrown.
Syntax
find_element_by_link_text("Coding Ground")
The find_element_by_partial_link_text() method is used to identify an element by partially matching with the text within the anchor tag as specified in the method parameter. If there is no matching text, NoSuchElementException is thrown.
Syntax −
find_element_by_partial_link_text("Coding")
Example
Code Implementation with find_element_by_link_text().
from selenium import webdriver driver = webdriver.Chrome (executable_path="C:\chromedriver.exe") # implicit wait for 5 seconds driver.implicitly_wait(5) # maximize with maximize_window() driver.maximize_window() driver.get("https://www.tutorialspoint.com/about/about_careers.htm") # identify element with link text and click() l=driver.find_element_by_link_text("Privacy Policy") l.click() driver.quit()
Example
Code Implementation with find_element_by_partial_link_text().
from selenium import webdriver driver = webdriver.Chrome (executable_path="C:\chromedriver.exe") # implicit wait for 5 seconds driver.implicitly_wait(5) # maximize with maximize_window() driver.maximize_window() driver.get("https://www.tutorialspoint.com/about/about_careers.htm") # identify element with partial link text and click() l=driver.find_element_by_partial_link_text("Privacy") l.click() driver.quit()
- Related Articles
- How to click button Selenium Python?
- How to click on a button with Javascript executor in Selenium with python?
- Using Selenium in Python to click/select a radio button.
- How to click a link whose href has a certain substring in Selenium?
- 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?
- Find and click element by title Python Selenium.
- How to click on a link with a Javascript executor in Selenium with python?
- Fetch all href link using selenium in python.
- Add a pressed effect on button click with CSS
- Hang Up Your iPhone with the Click of a Button
- How to add and remove names on button click with JavaScript?
- How to select a radio button in a page in Selenium with python?
- How to perform double click on an element in Selenium with python?
