如何从selenium中的href链接获取属性值

时间:2015-09-16 23:21:25

标签: c# selenium selenium-webdriver

我正试图从" a href"获取链接。属性

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

我在做什么:

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

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

我正在使用类&#34; dl_link 1&#34;但我无法得到它的链接,字符串是空的吗?

3 个答案:

答案 0 :(得分:14)

您需要使用实际属性名称调用GetAttribute()。替换:

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

使用:

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

答案 1 :(得分:2)

<强> C#

element.GetAttribute("attribute name");

<强>红宝石

element.attribute("attribute name")

<强>的Python

element.get_attribute("attribute name")

<强>爪哇

element.getAttribute("attribute name")

答案 2 :(得分:0)

在C#中,用于锚标记

string url = lists1[0].Url();

将产生与

相同的结果
string url = ists1[0].GetAttribute("href");

注意: 即使href在锚标记中具有相对路径,两者都返回完整的网址。

For <a href="/profile/my-profile-id" /> 

element.Url();
// returns http://mywebsite.com/profile/my-profile-id

element.GetAttribute("href"); 
// returns http://mywebsite.com/profile/my-profile-id
相关问题