C# 如何从selenium中的href链接获取属性值
我正在尝试从“a href”属性获取链接C# 如何从selenium中的href链接获取属性值,c#,selenium,selenium-webdriver,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.FindElemen
<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");
ReadOnlyCollection lists1=driver.FindElements(By.ClassName(“dl_-link”);
字符串s=lists1[0].GetAttribute(“a href”);
我正在获取类为“dl_link 1”的元素,但无法获取其链接,字符串为空?您需要使用实际属性名调用
GetAttribute()
。替换:
lists1[0].GetAttribute("a href");
与:
C#
红宝石
element.attribute("attribute name")
Python
element.get_attribute("attribute name")
Java
element.getAttribute("attribute name")
在C#中,用于锚定标记
string url = lists1[0].Url();
将产生与相同的结果
string url = ists1[0].GetAttribute("href");
注意:
两者都返回完整的Url,即使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
用于
Url()元素;
//返回http://mywebsite.com/profile/my-profile-id
element.GetAttribute(“href”);
//返回http://mywebsite.com/profile/my-profile-id
string url = ists1[0].GetAttribute("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