
- 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
How to click a href link using Selenium?
We can click a href link with Selenium webdriver. There are multiple ways to achieve this. First of all we need to identify the link with the help of the locators like link text and partial link text.
The link text locator identifies the element whose text matches with text enclosed within the anchor tag. The partial link text locator identifies the element whose text matches partially with the text enclosed within the anchor tag. Once the linkwith href is identified we need to apply the click() method to it.
Let's try to click the link Privacy Policy on the page.
Example
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 ClickLinkText{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify element with link text then apply click() WebElement p=driver.findElement(By.linkText("Privacy Policy")); p.click(); System.out.println("Page title after link click : " + driver.getTitle()); driver.close(); } }
Example
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 ClickPartialLinkText{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify element with link text then apply click() WebElement p=driver.findElement(By.partialLinkText("Privacy")); p.click(); System.out.println("Page title after link click : " + driver.getTitle()); driver.close(); } }
Output
- Related Questions & Answers
- Selenium Click Link By href Value
- How to click a link whose href has a certain substring in Selenium?
- How to click on a link in Selenium?
- Fetch all href link using selenium in python.
- How to click on a hyper link using linkText in Selenium?
- How to click on a link using Selenium webdriver in Python.
- Click a href button with Selenium and Python?
- How to click on a link in Selenium with python?
- How to get an attribute value from a href link in selenium?
- How to handle a link click event using jQuery?
- How to click on a link with a Javascript executor in Selenium with python?
- How can I get the href of elements found by partial link text using Selenium?
- How to create right click using selenium?
- HTML DOM Link href Property
- How to effectively hide Href From a Link in PHP?
Advertisements