I have 10 different pages contain different links. How to click on all the links?
Conditions are : i) I don't know how many links are there ii) I want to count and click on each link
Please suggest me Selenium WebDriver script.
A linkText is used to identify the hyperlinks on a web page. It can be determined with the help of an anchor tag (<a>). In order to create the hyperlinks on a web page, you can use anchor tags followed by the linkText.
Mastering XPath and CSS Selector for Selenium Testers might be in a situation to find all the links on a website. We can easily do so by finding all elements with the Tag Name "a", as we know that for any link reference in HTML, we need to use "a" (anchor) tag.
We can click on a link on page with the help of locators available in Selenium. Link text and partial link text are the locators generally used for clicking links. Both these locators work with the text available inside the anchor tags. Link Text – The text within the anchor tag is matched with the element to be identified.
Selenium WebDriver provides two different identifiers to interact with links. linktext () identifier matches exact text associated with the link web element. So, to click on above-mentioned link code will be: partialLinktext () identifier matches partial text associated with the link web element. It can be considered as a contains method.
Here in above code, “ href “ is an attribute which specifies the address of another HTML page and “Selenium Tutorial” is the link text associated with it. (i.e. the visible part on the page). Selenium WebDriver provides two different identifiers to interact with links.
Using selenium, we can find elements by class, by css selector, by id, by link text, by partial link text, by name, or by tag name. This is shown in the table below.
Capture and Navigate all the Links on Webpage
Iterator and advanced for loop can do similar job; However, the inconsistency on page navigation within a loop can be solved using array concept.
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| Capture all links under specific frame|class|id and Navigate 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| Capture all links [Alternate method]
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);
}
}
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()
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 a link, navigates back to the page and clicks the second link again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With