Java Selenium how to get linkText (anchor) from WebElement link

I have a WebElement containing a link found by url. I can extract the url:

element.getAttribute("href");

      

But the question is this: how to extract its anchor, I try like this:

webElement.getAttribute("linkText");

      

It gives me a null value. I'm 100% sure this link has an anchor. Is there a way to get the anchor? It's more complicated, but a simplified code example might look like this:

WebDriver driver = new FirefoxDriver();
        driver.get("http://stackoverflow.com/questions/tagged/java");
        WebElement link  = driver.findElement(By.linkText("Bicycles"));

        System.out.println(link.getAttribute("href")); // shows http://bicycles.stackexchange.com/
        System.out.println(link.getAttribute("linkText")); // shows null

      

+3


source to share


3 answers


Try this time



System.out.println(link.getText());

+2


source


By "Anchor" I think you mean link text? If so, you can use .getText()

since it <a>

is a block level element.



link.getText();

      

+2


source


Here you can save your text id: String Text = driver.findElement (By.id ("Text")). GetText (); System.out.println (text);

+1


source







All Articles