我想在中找到此链接“我们的状态”的元素<h5>
。我正在craigslist中尝试此操作。任何帮助将不胜感激
这是网址:http : //auburn.craigslist.org/
<html class="">
<head>
<body class="homepage w1024 list">
<script type="text/javascript">
<article id="pagecontainer">
<section class="body">
<table id="container" cellspacing="0" cellpadding="0"
<tbody>
<tr>
<td id="leftbar">
<td id="center">
<td id="rightbar">
<ul class="menu collapsible">
<li class="expand s">
<li class="s">
<li class="s">
<h5 class="ban hot">us states</h5>
<ul class="acitem" style="display: none;">
</li>
<li class="s">
<li class="s">
在您的情况下,仅使用类名是不够的。
By.cssSelector(".ban")
有15个匹配节点By.cssSelector(".hot")
有11个匹配节点By.cssSelector(".ban.hot")
有5个匹配节点因此,您需要更多限制来缩小范围。下面的选项1和2可用于CSS选择器,其中1可能是最适合您的需求的选择器。
选项1:使用列表项的索引(CssSelector或XPath)
局限性
例子:
driver.FindElement(By.CssSelector("#rightbar > .menu > li:nth-of-type(3) > h5"));
driver.FindElement(By.XPath("//*[@id='rightbar']/ul/li[3]/h5"));
选项2:使用Selenium的FindElements
,然后对其进行索引。(CssSelector或XPath)
局限性
例子:
// note that By.CssSelector(".ban.hot") and //*[contains(@class, 'ban hot')] are different, but doesn't matter in your case
IList<IWebElement> hotBanners = driver.FindElements(By.CssSelector(".ban.hot"));
IWebElement banUsStates = hotBanners[3];
选项3:使用文字(仅XPath)
局限性
例子:
driver.FindElement(By.XPath("//h5[contains(@class, 'ban hot') and text() = 'us states']"));
选项4:索引分组的选择器(仅XPath)
局限性
例子:
driver.FindElement(By.XPath("(//h5[contains(@class, 'ban hot')])[3]"));
选项5:通过href找到隐藏的列表项链接,然后遍历到h5(仅XPath)
局限性
例子:
driver.FindElement(By.XPath(".//li[.//ul/li/a[contains(@href, 'geo.craigslist.org/iso/us/al')]]/h5"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句