How to click on All links on a web page

I have 10 different pages that contain different links. How to click on all links?

Conditions: I do not know how many links are there. I want to count and click on each link

Please offer me a selenium webdriver script.

+3
source share
6 answers

Capturing and moving all links on a web page

An iterator and an advanced for loop can do the same job; However, inconsistencies in page navigation in a loop can be resolved using the concept of an array.

private static String[] links = null; private static int linksCount = 0; driver.get("www.xyz.com"); List<WebElement> linksize = driver.findElements(By.tagName("a")); linksCount = linksize.size(); System.out.println("Total no of links Available: "+linksCount); links= new String[linksCount]; System.out.println("List of links Available: "); // print all the links from webpage for(int i=0;i<linksCount;i++) { links[i] = linksize.get(i).getAttribute("href"); System.out.println(all_links_webpage.get(i).getAttribute("href")); } // navigate to each Link on the webpage for(int i=0;i<linksCount;i++) { driver.navigate().to(links[i]); Thread.sleep(3000); } 

1 | Capturing all links under a specific frame | class | id and move one by one

 driver.get("www.xyz.com"); WebElement element = driver.findElement(By.id(Value)); List<WebElement> elements = element.findElements(By.tagName("a")); int sizeOfAllLinks = elements.size(); System.out.println(sizeOfAllLinks); for(int i=0; i<sizeOfAllLinks ;i++) { System.out.println(elements.get(i).getAttribute("href")); } for (int index=0; index<sizeOfAllLinks; index++ ) { getElementWithIndex(By.tagName("a"), index).click(); driver.navigate().back(); } public WebElement getElementWithIndex(By by, int index) { WebElement element = driver.findElement(By.id(Value)); List<WebElement> elements = element.findElements(By.tagName("a")); return elements.get(index); } 

2 | Capturing all links [Alternative Method]

Java

 driver.get(baseUrl + "https://www.google.co.in"); List<WebElement> all_links_webpage = driver.findElements(By.tagName("a")); System.out.println("Total no of links Available: " + all_links_webpage.size()); int k = all_links_webpage.size(); System.out.println("List of links Available: "); for(int i=0;i<k;i++) { if(all_links_webpage.get(i).getAttribute("href").contains("google")) { String link = all_links_webpage.get(i).getAttribute("href"); System.out.println(link); } } 

Python

 from selenium import webdriver driver = webdriver.Firefox() driver.get("https://www.google.co.in/") list_links = driver.find_elements_by_tag_name('a') for i in list_links: print i.get_attribute('href') driver.quit() 
+7
source
 public static void main(String[] args) { FirefoxDriver fd=new FirefoxDriver(); fd.get("http:www.facebook.com"); List<WebElement> links=fd.findElements(By.tagName("a")); System.out.println("no of links:" +links.size()); for(int i=0;i<links.size();i++) { if(!(links.get(i).getText().isEmpty())) { links.get(i).click(); fd.navigate().back(); links=fd.findElements(By.tagName("a")); } } } 

This program clicks on the link, moves to the page and clicks the second link again.

+1
source

Save them in an array and click on them:

  ArrayList<WebElement> input_type = (ArrayList<WebElement>) driver.findElements(By.tagName("a")); for (WebElement type : input_type) { type.click(); } 

This will click one by one all the links with the tag, I hope you understand the meaning. Enjoy it!

0
source
 import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.internal.ProfilesIni; public class Find_all_Links { private static String testUrl = "http://www.google.co.in/"; private static WebDriver driver = null; public static void main(String[] args) { ProfilesIni profile = new ProfilesIni(); FirefoxProfile myProfile = profile.getProfile("AutomationQA"); driver = new FirefoxDriver(myProfile); driver.get(testUrl); List<WebElement> oLinksOnPage = driver.findElements(By.tagName("a")); System.out.println(oLinksOnPage.size()); for(int i=0;i<oLinksOnPage.size();i++){ System.out.println(oLinksOnPage.get(i).getText()); } } } 
0
source
 package selenium.tests; import java.util.List; import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class TestAllLinks { public static void main(String[] args) { String baseUrl = "http://www.qaautomated.com/"; System.setProperty("webdriver.chrome.driver", "C:\\Users\\chromedriver_win32\\chromedriver.exe"); WebDriver driver=new ChromeDriver(); String notWorkingUrlTitle = "Under Construction: QAAutomated"; driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get(baseUrl); List<WebElement> linkElements = driver.findElements(By.tagName("a")); String[] linkTexts = new String[linkElements.size()]; int i = 0; //extract the link texts of each link element for (WebElement elements : linkElements) { linkTexts[i] = elements.getText(); i++; } //test each link for (String t : linkTexts) { driver.findElement(By.linkText(t)).click(); if (driver.getTitle().equals(notWorkingUrlTitle )) { System.out.println("\"" + t + "\"" + " is not working."); } else { System.out.println("\"" + t + "\"" + " is working."); } driver.navigate().back(); } driver.quit(); } } 

http://www.qaautomated.com/2016/10/selenium-test-to-check-links-in-web.html

0
source

You can use 2 logics to process

  • Get the link and click it in your browser and confirm
  • Get the link and use the REST web service to check the link.

Using REST WS would be the easiest way to verify links.

Below code works fine for me.

 public class Testing{ public static void main(String[] args) { try{ WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); driver.navigate().to("https://www.amazon.in/"); List<WebElement> links = driver.findElements(By.tagName("a")); System.out.println("Number of links : " + links.size()); for(WebElement e : links) { String linkText = e.getAttribute("href"); System.out.println("Link -->>" +linkText); if(linkText!=null && !linkText.isEmpty()) { HttpPost post = new HttpPost(linkText); HttpClient client = HttpClientBuilder.create().build(); HttpResponse res = client.execute(post); String s = res.getStatusLine().toString(); if(s.equals("HTTP/1.1 200 OK")) { System.out.println("Navigated"); //your code to handle logic } else { //your code to handle logic with other response code } } } } catch (Exception e) { System.out.println(e.getStackTrace()); } } } 
0
source

All Articles