用selenium获得href实际值

时间:2018-03-19 14:30:54

标签: c# google-chrome selenium selenium-webdriver canonical-link

我必须检查页面的规范标签,但是当href为空时我遇到了问题。 这是代码:

ChromeOptions chromeCapabilities = new ChromeOptions();
chromeCapabilities.AddArguments("disable-infobars");
IWebDriver webDriver = new ChromeDriver(chromeCapabilities);
webDriver.Manage().Window.Maximize();
webDriver.Navigate().GoToUrl("https://www.example.com/subpage/page");

List <IWebElement> linkElements = webDriver.FindElements(By.TagName("link")).ToList();
string canonicalHref = linkElements.Find(x => String.Compare(x.GetAttribute("rel"), "canonical") == 0).GetAttribute("href");

//debug
var html = linkElements.Find(x => String.Compare(x.GetAttribute("rel"), "canonical") == 0);
Console.WriteLine(html.GetAttribute("outerHTML")); //<link href="" rel="canonical" />
Console.WriteLine(html.GetAttribute("href")); // should be "" but I get https://www.example.com/subpage/page
Console.WriteLine(html.GetAttribute("rel")); //canonical       

Console.WriteLine(canonicalHref); // should be "" but I get https://www.example.com/subpage/page

错误的设置示例:

enter image description here

我得到的网址不是空字符串......但为什么?我拨错了属性吗?有什么想法才能获得真正的价值吗?

1 个答案:

答案 0 :(得分:1)

方法string href = (string)((IJavaScriptExecutor)driver).ExecuteScript( "return arguments[0].getAttribute('href') || '';", link) 返回DOM属性(如果存在)或HTML属性(如果属性丢失)。看起来页面改变了属性。

要获取HTML属性,您必须使用脚本注入:

A_City,QQQQ
B_State,QQQQ
C_Country,QQQQ
A_Cityt,YYYY
B_State,YYYY
C_Country,YYYY