Get link text Selenium java

11,377

Solution 1

Hi 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")));
}

Solution 2

To locate the element you can use

List<WebElement> elements = driver.findElements(By.partialLinkText("ZZ"));
// or
List<WebElement> elements = driver.findElements(By.cssSelector("[href*='ZZ']"));

To get the href and text you can do

for (WebElement element : elements) {
    String href = element.getAttribute("href");
    String text = element.getText();
    // or
    String text = element.getAttribute("innerText");

    // and to get the number
    String[] data = text.split("-");
    String number = data[1];
}
Share:
11,377
Michal
Author by

Michal

Senior QA Automation Engineer, learning Machine Learning in free time using Kaggle Competitions

Updated on September 11, 2022

Comments

  • Michal
    Michal almost 2 years

    I have problem with getting text of link.

    On site I have text link <a href="DetailsZZ-10048.html">ZZ-10048</a>, 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 get on output Printing null everytime I changed and looked for a solution

    And the solution: Java Selenium, how to get linkText (anchor) from link WebElement is not good also, 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-)

  • Michal
    Michal over 8 years
    it works almost great, I just get the NullPointerException on the String[] data = text.split("-"); line, so I'm making to find out what's happens :)
  • Michal
    Michal over 8 years
    works almost great, I think I know what you want to show :) I just got "java.lang.ArrayIndexOutOfBoundsException: 1" error on the return, so I just need to find out what's happend (but thanks for a rod :) )
  • Subh
    Subh over 8 years
    @Michal : I am guessing there are some a element(s) without the numerical value in their href attribute; something like: "DetailsZZ" or "DetailsZZ-" or "<some other random text>", and that's why it throws ArrayOutOfBoundsException since there is no second value in the array to display. Try in the above code, as @Andersson commented, by replacing with either of these List<WebElement> elements = driver.findElements(By.xpath("//a[contains(@href, "DetailsZZ-")]")); or List<WebElement> elements = driver.findElements(By.xpath("//a[starts-with(href, "DetailsZZ-")]"));
  • Guy
    Guy over 8 years
    @Michal with both versions of text extracting?
  • Michal
    Michal over 8 years
    You guys are great, that's what it should do! :D Thank you! But for the future if somebody will look here, it should be 'DetailsZZ-' instead of "DetailsZZ-" :)
  • Ankur Gupta
    Ankur Gupta over 8 years
    @Michael....thats sounds an array problem to me i.e Array[1]...it may happen when the value does not exist for an array and we trying to fetch it..
  • Markus
    Markus about 7 years
    I am not sure, but element.element.getText() should not compile because element is not a public field in WebElement.