Issue
I have a problem with getting the text of a link.
On a site, I have the text link
<a href="DetailsZZ-10048.html">ZZ-10048</a>
. The part with ZZ-
is static, the number increments and it isn't known for me earlier. I need to get this number.
I used looking at: Get link text - Selenium, Java, but there I have all links, URLs (not the text of the links).
I also tried: How to gettext() of an element in Selenium Webdriver, but I got output Printing null every time I changed and looked for a solution.
And the solution: Java Selenium, how to get linkText (anchor) from link WebElement is not good either, because it doesn't recognise "a[href*='ZZ-']"
.
So, the closest one is:
List<WebElement> elements = driver.findElements(By.tagName("a"));
for (int i = 0; i < elements.size(); i++) {
System.out.println(elements.get(i).getAttribute("href"));
}
But how can I change to view not only URLs, but names of the link? (especially one which starts from ZZ-
)
Solution
You can use the following code to extract the number:
public String splitfunc(String str)
{
str = str.replace(".html", "");
String[] array = str.split("-");
return array[1];
}
List<WebElement> elements = driver.findElements(By.tagName("a"));
for (int i = 0; i < elements.size(); i++) {
System.out.println(splitfunc(elements.get(i).getAttribute("href")));
}
Answered By - Ankur Gupta
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.