I have a WebElement containing link found by url. I can extract url by:
element.getAttribute("href");
But the question is: how to extract it's anchor, I'm trying like this:
webElement.getAttribute("linkText");
It gives me null value. I'm 100% sure this link has an anchor. Is there any way to get anchor ? It's more complicated, but example simplified code could 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
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.
We can find an element using the link text or the partial link text in Selenium webdriver. Both these locators can only be applied to elements with the anchor tag. The link text locator matches the text inside the anchor tag. The partial link text locator matches the text inside the anchor tag partially.
Using Link Text In Selenium To Locate An Element In order to access link using link text in Selenium, the below-referenced code is used: driver. findElement(By. linkText("this is a link text"));
If getText() returns an empty String, try the innerHTML attribute:
String text = element.getAttribute("innerHTML")
Try this:
System.out.println(link.getText());
By "Anchor" I think you mean the text of the link? If so, then you can use .getText()
since an <a>
is a block level element.
link.getText();
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