

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Selenium Click Link By href Value
We can click a link by href value with Selenium webdriver. To identify the link with the href attribute we can take the help of the locators xpath or css selector.
We shall use the findElement method and pass By.xpath or By.cssSelector as a parameter to this method. Let us investigate the html code of the link Privacy Policy in the below image with the anchor tag having a href attribute as /about/about_privacy.htm.
Code Implementation
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class FirstAssign{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "chromedriver"); WebDriver driver = new ChromeDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get ("https://www.tutorialspoint.com/about/about_privacy.htm"); //identify element with href value in CSS WebElement elm = driver .findElement(By.cssSelector("a[href*='about_privacy']")); String s = elm.getText(); System.out.println("Text is: " + s); //browser close driver.quit(); } }
Output
- Related Questions & Answers
- How to click a href link using Selenium?
- How to click a link whose href has a certain substring in Selenium?
- Click a href button with Selenium and Python?
- How to get an attribute value from a href link in selenium?
- Fetch all href link using selenium in python.
- How to click on a link in Selenium?
- How can I get the href of elements found by partial link text using Selenium?
- HTML DOM Link href Property
- How to click on a link in Selenium with python?
- HTML <link> href Attribute
- How to click on a hyper link using linkText in Selenium?
- How to click on a link using Selenium webdriver in Python.
- Find and click element by title Python Selenium.
- JavaScript - Get href value
- How to search the value of the href attribute of a link in JavaScript?
Advertisements