- 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
How to click a link whose href has a certain substring in Selenium?
We can click a link whose href has a certain substring in the Selenium webdriver. There are multiple ways to perform this action. We can use find_element_by_partial_link_text() method to click on a link having a substring.
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("Tutors")
Also we can use css selector to identify the element whose href has a substring via * symbol. This is called wild character notation and implies that a string contains a certain substring.
Syntax −
find_element_by_css_selector("a[href*"='tutor'] ")
Let us try to identify the Net Meeting link in the above html code with the substring concept.
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/codingground.htm") # identify element with partial link text and click() l=driver.find_element_by_partial_link_text("Net") l.click() print("Page title after click on substring link name:" + driver.title) driver.quit()
Example
Code Implementation with href attribute.
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/codingground.htm") # identify element with href attribute and click() l= find_element_by_css_selector("a[href*"='net'] ") l.click() print("Page title after click on substring link name:" + driver.title) driver.quit()
Output
- Related Articles
- How to click on a link in Selenium?
- How to click on a link in Selenium with python?
- Click a href button with Selenium and Python?
- How to get an attribute value from a href link in selenium?
- How to click on a hyper link using linkText in Selenium?
- How to click on a link using Selenium webdriver in Python.
- How to click on a link with a Javascript executor in Selenium with python?
- Fetch all href link using selenium in python.
- How to disable a href link in CSS?
- How to effectively hide Href From a Link in PHP?
- How to find the href attribute of a link in a document in JavaScript?
- How to handle a link click event using jQuery?
- How to find the href and target attributes in a link in JavaScript?
- How to link back out of a folder using the a-href tag?
- How can I get the href of elements found by partial link text using Selenium?
