XPath来获得列的表
问题描述:
我想确认里面从下面的HTML代码 “IMG” 的属性[请参考图片]XPath来获得列的表
代码这里
//表[@ class ='row header-logo'and @ style ='border-collapse:collapse; border-spacing:0; display:table; padding:0; position:relative; text-align:left; vertical-align:top; width :100%']
请帮助我得到确切的x路径验证由标题,ALT,SRC
答
第一 “IMG” 的属性,与这个XPath定位<img>
:
//img[@alt='Intel']
解释:
-
//
- 选择第一匹配的节点 -
img[@alt='Intel']
- 请选择<img>
其中alt
等于字符串'Intel'
然后,使用IWebElement.GetAttribute
得到<img>
属性来验证:
IWebDriver driver;
try
{
// locate image
IWebElement image = driver.FindElement(By.XPath("//img[@alt='Intel']"));
// get image attributes
string title = image.GetAttribute("title");
string alt = image.GetAttribute("alt");
string src = image.GetAttribute("src");
// validate title, alt, and src
// EX: Assert.AreEqual("Intel", title);
}
catch (NoSuchElementException)
{
// image does not exist, handle accordingly
}
HTML是不标准的XML,所以XPath是不能保证。尝试其他库,检查[在C#中解析HTML的最佳方式是什么?](https://*.com/questions/56107/what-is-the-best-way-to-parse-html-in-c ) –
谢谢雷洋。请向我建议一些图书馆名称,因为您分享的参考信息有很多信息,这些信息令人困惑: – Dhanaprabhu
挑选最高票数 –