需要通过css在selenium中查找元素 [英] Need to find element in selenium by css
问题描述
我想在< h5>
中找到此链接us states的元素。我在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 =containercellspacing =0cellpadding =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 =acitemstyle =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)
限制
- 如果网站结构发生变化,则不够稳定
p>
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)
示例:
//注意By.CssSelector(。ban.hot)和/ / * [contains(@class,'ban hot')]是不同的,但是在你的case不重要
IList< IWebElement> hotBanners = driver.FindElements(By.CssSelector(。ban.hot));
IWebElement banUsStates = hotBanners [3];
选项3:使用文字(仅限XPath) b
$ b
限制
- 不适用于多语言网站
- 仅适用于XPath,不适用于Selenium的CssSelector
示例: $ b
driver.FindElement(By.XPath(// h5 [contains(@class,'ban hot')and text()='us states ']));
选项4:对分组选择器进行索引(仅限XPath) >
限制
- 如果网站结构发生变化, / li>
- 仅适用于XPath,不适用于CssSelector
p>
driver.FindElement(By.XPath(h5 [contains(@class,'ban hot')])[3 ]));
选项5:通过href查找隐藏的列表项链接,然后遍历回h5 XPath only)
- 仅适用于XPath,不适用于CssSelector
- 低性能
- Tricky XPath
例如:
driver.FindElement(By.XPath //li[/////h/a[contains(@href,'geo.craigslist.org/iso/us/al')]]/h5));
I want to find the element of this link "us states" in <h5>
. I am trying this in craigslist. Any help will be highly appreciated
Here is the url: 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">
解决方案 Only using class names is not sufficient in your case.
By.cssSelector(".ban")
has 15 matching nodes
By.cssSelector(".hot")
has 11 matching nodes
By.cssSelector(".ban.hot")
has 5 matching nodes
Therefore you need more restrictions to narrow it down. Option 1 and 2 below are available for css selector, 1 might be the one that suits your needs best.
Option 1: Using list items' index (CssSelector or XPath)
Limitations
- Not stable enough if site's structure changes
Example:
driver.FindElement(By.CssSelector("#rightbar > .menu > li:nth-of-type(3) > h5"));
driver.FindElement(By.XPath("//*[@id='rightbar']/ul/li[3]/h5"));
Option 2: Using Selenium's FindElements
, then index them. (CssSelector or XPath)
Limitations
- Not stable enough if site's structure changes
- Not the native selector's way
Example:
// 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];
Option 3: Using text (XPath only)
Limitations
- Not for multilanguage sites
- Only for XPath, not for Selenium's CssSelector
Example:
driver.FindElement(By.XPath("//h5[contains(@class, 'ban hot') and text() = 'us states']"));
Option 4: Index the grouped selector (XPath only)
Limitations
- Not stable enough if site's structure changes
- Only for XPath, not CssSelector
Example:
driver.FindElement(By.XPath("(//h5[contains(@class, 'ban hot')])[3]"));
Option 5: Find the hidden list items link by href, then traverse back to h5 (XPath only)
Limitations
- Only for XPath, not CssSelector
- Low performance
- Tricky XPath
Example:
driver.FindElement(By.XPath(".//li[.//ul/li/a[contains(@href, 'geo.craigslist.org/iso/us/al')]]/h5"));
这篇关于需要通过css在selenium中查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!