硒:如何通过部分href查找元素? [英] Selenium: How to find element by partial href?
本文介绍了硒:如何通过部分href查找元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
工作代码1:
Driver.Instance.FindElement( By.XPath("//a[contains(@href,'" + PartialLinkHref + "')]" ));
工作代码2:
ReadOnlyCollection<IWebElement> linkList = Driver.Instance.FindElements(By.TagName("a"));
for (int i = 0; i < linkList.Count ; i++)
{
if (linkList[1].GetAttribute("href").Contains(PartialLinkHref))
{
element.SetElement(linkList[i]);
return element;
break;
}
}
推荐答案
初始选择器的问题是您缺少选择器前面的//
. //
告诉XPath搜索整个html树.
The problem with your initial selector is that you're missing the //
in front of the selector. the //
tells XPath to search the whole html tree.
这应该可以解决问题:
Driver.Instance.FindElement(By.XPath("//a[contains(@href, 'long')]"))
如果要查找元素的子代,请改用.//
,例如
If you want to find children of an element, use .//
instead, e.g.
var element = Driver.Instance.FindElement("..some selector..")
var link = element.FindElement(".//a[contains(@href, 'long')]"))
如果要查找包含文本而不是href属性的链接,则可以使用
If you want to find a link that contains text and not by the href attribute, you can use
Driver.Instance.FindElement(By.XPath("//a[contains(text(), 'long')]"))
这篇关于硒:如何通过部分href查找元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文