Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get an attribute value from a href link in selenium

i am trying to get the link from "a href" attribute

<a href="http://fgkzc.downloader.info/download.php?id=bc56585624bbaf29ebdd65d0248cb620" rel="nofollow" class="dl_link 1" style="">Download</a>

what i am doing:

ReadOnlyCollection<IWebElement> lists1 = driver.FindElements(By.ClassName("dl_link"));

string s = lists1[0].GetAttribute("a href");

i am getting the element with class "dl_link 1" but i can't get it's link, the string is null?

like image 627
user3181034 Avatar asked Sep 16 '15 23:09

user3181034


People also ask

What method can be used to retrieve the href attribute of an element?

What method can be used to retrieve the href attribute of an element? Answer: Use the jQuery . attr() Method attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.

How does Selenium find element by link?

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

Is href a property or attribute?

Definition and UsageThe 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.


1 Answers

You need to call GetAttribute() with actual attribute name. Replace:

lists1[0].GetAttribute("a href");

with:

lists1[0].GetAttribute("href");
like image 112
alecxe Avatar answered Oct 03 '22 11:10

alecxe