
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Link with Specific 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()