How to extract no. of links and text
of links present on a page in Selenium Web Driver
AS you know that name of tag for all
link start with the letter 'a'. 'a' refer to the anchor tag. The name
of tag which start with the letter 'a' must be a link
All all links have name of tags 'a'
therefore you can identify the link with the name of tag function of
By class for example
driver.findElements(By.tagName('a'));
On page to confirm the Total Nos of
links :
Assume we like to search the total nos
of link on the page of Google for this we can use the following:
driver.get(“www.google.com”);
List<WebElement>
alllinkspresent = driver.findElements(By.tagName(“a”));
System.out.println(“nos of link on
the page of google are : “+alllinkpresent.size());
Where size()
function return the nos of total links from the Google page.
To search or print the links name or
example text of links:
Assume
we like to print the text of all the links present on the page of
Google for these we can use
for (int i=0;
i<alllinkspresent.size(); i++)
{
system.out.println(alllinkspresent.get(i).getText());
}
Extracting the links from the
specific section:
Assume we link to
extract the link of language offered by the Google Section for this
we can use the following:
WebElement
langSection = driver.findElement(By.xpath(“//*[@id = 'addlang']”));
List<WebElement>
lang = LangSection.findelements(By.tagname(“a”));
for (int i=0;
i<lang.size(); i++)
{
System.out.println(lang.get(i).getText());
}
Where Lang Section
is one Web Element and you have to search the all the present links
in this section. For understand of the code of this section –
Please check the following attached screens:
Links Present on page in selenium web driver |
Now when above
code will be executed by us, We will get the text for all the link
available in this section. For more details plz check the following
screen:
Result Text link present in Selenium Web Drivers |
No comments:
Post a Comment