Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How do I pass options to the Selenium Chrome driver using java?
Next article icon

How to Get All Available Links on the Page using Selenium in Java?

Last Updated : 23 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Selenium is an open-source Web-Automation tool that is used to automate web Browser Testing. The major advantage of using selenium is, that it supports all major web browsers and works on all major Operating Systems, and it supports writing scripts on various languages such as Java,  JavaScript, C# and Python, etc. While automating a webpage, we are required to fetch and check all the available links present on the webpage, In this article, we will learn to get all the available links present on a page using "TagName".

As we know all the links are of type anchor tag "a" in HTML. For Example,

<a href="geeksforgeeks.org">geeksforgeeks</a>

Table of Content

  • How to fetch all the links on a webpage?
  • Sample Code Example to Scrape Links

How to fetch all the links on a webpage?

  • Navigate to the webpage.
  • Get the list of WebElements with the TagName "a".
  • List<WebElement> links=driver.findElements(By.tagName("a"));
  • Iterate through the List of WebElements.
  • Print the link text.

Sample Code Example to Scrape Links

In this example, we are navigating to the URL "https://www.geeksforgeeks.org/" and print the link text of all available links on the page.

Java
public class Geeks {      WebDriverManager.chromedriver().setup();     WebDriver driver = new ChromeDriver();     driver.manage().window().maximize();     driver.get("https://www.geeksforgeeks.org/");      // Get all the available Links     List<WebElement> links         = driver.findElements(By.tagName("a"));      // Iterating through all the Links and printing link     // text     for (WebElement link : links) {         System.out.println(link.getText());     }      driver.close(); } 

Output:

This program will get all the Links in the List of WebElements and print all the link texts.


Next Article
How do I pass options to the Selenium Chrome driver using java?

A

allwink45
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Software Testing
  • Selenium
  • Technical Scripter 2022
  • selenium
Practice Tags :
  • Java

Similar Reads

  • How to get the total number of checkboxes in a page using Selenium?
    In the world of automated testing, Selenium is a powerful tool that helps automate web browsers. One common task during web testing is determining the number of specific elements on a page, such as checkboxes. This can be useful for validating the presence and quantity of checkboxes on a form or any
    3 min read
  • How to check if an image is displayed on page using Selenium?
    When it comes to web automation and testing, It is important to confirm that a picture has been rendered on a particular page. This is because bad content loading is regarded as a big issue in the good user interface. Users can easily get frustrated with missing or broken images, hence the need for
    3 min read
  • How to Open Microsoft Edge Browser using Selenium in Java?
    Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is, it supports all browsers like Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari, works on all major OS and its scripts are written in various languages i.e Java, Python, JavaScript, C#, e
    3 min read
  • How do I pass options to the Selenium Chrome driver using java?
    Selenium WebDriver is a widely used tool for automating web browsers. When working with Selenium ChromeDriver, passing custom options to the browser allows you to tailor the browsing experience based on your test requirements. Whether you're running tests in headless mode, disabling notifications, o
    2 min read
  • How to automate Instagram login page using java in Selenium?
    Automating the Instagram login page using Java and Selenium WebDriver is a crucial task for testing and automating web interactions. Instagram, being one of the most popular social media platforms, requires a precise approach to logging in and interacting with its elements. In this guide, we will wa
    4 min read
  • How to Open Chrome Browser Using Selenium in Java?
    Selenium is an open-source popular web-based automation tool. The major advantage of using selenium is, it supports all browsers like Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari, works on all major OS, and its scripts are written in various languages i.e Java, Python, JavaScript, C#,
    3 min read
  • How to Handle Alert in Selenium using Java?
    Imagine filling out a form online and accidentally missing some information. You only know if you made a mistake if the website tells you somehow, like with a pop-up message. This article explains what those pop-up messages are called in Selenium (alerts) and how to deal with them in your automated
    6 min read
  • How to Run Gecko Driver in Selenium Using Java?
    Selenium is a well-known software used for software testing purposes. It consists of three parts: Selenium IDE, Selenium WebDriver, and Selenium Grid. Selenium WebDriver is the most important. Using WebDriver, online website testing can be done. There are three main WebDriver implementations: Chrome
    5 min read
  • How to get an attribute value from a href link in Selenium java?
    When working with Selenium WebDriver in Java, automating web interactions often involves handling hyperlinks. A common task is to extract the attribute value from a <a> tag, specifically the href attribute, which contains the link's destination URL. Extracting this href value allows you to ver
    3 min read
  • How to get the total number of radio buttons on a page using Selenium?
    When automating web testing with Selenium, one common task is to count the total number of radio buttons on a page. Radio buttons are crucial for selecting one option from a set of choices, and ensuring they are correctly displayed can be important for validating form functionality. In this guide, w
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences