ktm*_*cks 29 selenium webdriver css-selectors selenium-webdriver
我想找到这个链接"us states"的元素<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">
Run Code Online (Sandbox Code Playgroud)
Yi *_*eng 69
在您的情况下,仅使用类名是不够的.
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"));
Run Code Online (Sandbox Code Playgroud)
选项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];
Run Code Online (Sandbox Code Playgroud)
选项3:使用文本(仅限XPath)
限制
例:
driver.FindElement(By.XPath("//h5[contains(@class, 'ban hot') and text() = 'us states']"));
Run Code Online (Sandbox Code Playgroud)
选项4:为分组选择器编制索引(仅限XPath)
限制
例:
driver.FindElement(By.XPath("(//h5[contains(@class, 'ban hot')])[3]"));
Run Code Online (Sandbox Code Playgroud)
选项5:通过href查找隐藏列表项链接,然后遍历回h5(仅限XPath)
限制
例:
driver.FindElement(By.XPath(".//li[.//ul/li/a[contains(@href, 'geo.craigslist.org/iso/us/al')]]/h5"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
176466 次 |
| 最近记录: |