I am using selenium to get href attribute, lLinks are my web element that has a "href" attribute.
String url = lLinks.getAttrbute("href");
If the href of my 'a' tag is a relative path like <a href='/home'>Home</a>
, the url will return http://www.domain.com/home.
How do I get the url to just equal to the exact text of the href attribute?
To get href with Python BeautifulSoup, we can use the find_all method. to create soup object with BeautifulSoup class called with the html string. Then we find the a elements with the href attribute returned by calling find_all with 'a' and href set to True .
The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!
The HREF is an attribute of the anchor tag, which is also used to identify sections within a document. The HREF contains two components: the URL, which is the actual link, and the clickable text that appears on the page, called the "anchor text."
I think you cannot get that "href". Selenium only provides a full path or a relative path. See code below:
String href = linx.getAttribute("href");
System.out.println("Text is" + href);
String pathName = linx.getAttribute("pathname");
System.out.println("Text is" + pathName);
// Results
// Text is http://www.amazon.com/gp/yourstore/home/ref=nav_cs_ys/180-1519742-0316250
// Text is /gp/yourstore/home/ref=nav_cs_ys/180-1519742-0316250
You can obtain href
attribute by reading the whole element:
lLinks.getAttribute("outerHTML")
Which gives you e.g.:
<a id="button-id" href="#">Click me!</a>
Then you can use pattern matching to get the href
attribute.
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