selenium 对应 xpath 的 CSS 定位器
我正在测试的网页 html 的某些部分看起来像这样,
<div id="twoWideCallouts">
<div class="callout">
<a target="_blank" href="http://facebook.com">Facebook</a>
</div>
<div class="callout last">
<a target="_blank" href="http://youtube.com">Youtube</a>
</div>
我必须使用 selenium 检查,当我单击文本时,打开的 URL 与 href 中给出的 URL 相同,而不是错误页面。
使用 Xpath 我编写了以下命令
//i is iterator
selenium.getAttribute("//div[contains(@class, 'callout')]["+i+"]/a/@href")
但是,这非常慢并且对于某些链接不起作用。通过阅读这个网站上的许多答案和评论,我开始知道 CSS 定位器维护起来更快、更干净,所以我再次将其写为
css = div:contains(callout)
首先,我无法到达锚标记。
其次,该页面可以有任意数量的 div,其中 id = callout。使用 xpathcount 我可以得到这个计数,我将迭代该计数并执行 href 检查。如何使用 CSS 定位器完成类似的事情?
任何帮助将不胜感激。
编辑
我可以使用定位器 css=div.callout a
单击链接,但是当我尝试使用 String str = "css= 读取 href 值时div.callout a[href]"; selenium.getAttribute(str);
。我收到错误 - 未找到元素。下面给出控制台说明。
19:12:33.968 INFO - Command request: getAttribute[css=div.callout a[href], ] on session
19:12:33.993 INFO - Got result: ERROR: Element css=div.callout a[href not found on session
我尝试使用 xpath 获取 href 属性,如下所示
"xpath=(//div[contains(@class, 'callout')])["+1+"]/a/@href" and it worked fine.
请告诉我相应的 CSS 定位器应该是什么。
The some part of the html of the webpage which I'm testing looks like this
<div id="twoWideCallouts">
<div class="callout">
<a target="_blank" href="http://facebook.com">Facebook</a>
</div>
<div class="callout last">
<a target="_blank" href="http://youtube.com">Youtube</a>
</div>
I've to check using selenium that when I click on text, the URL opened is the same that is given in href and not error page.
Using Xpath I've written the following command
//i is iterator
selenium.getAttribute("//div[contains(@class, 'callout')]["+i+"]/a/@href")
However, this is very slow and for some of the links doesn't work. By reading many answers and comments on this site I've come to know that CSS loactors are faster and cleaner to maintain so I wrote it again as
css = div:contains(callout)
Firstly, I'm not able to reach to the anchor tag.
Secondly, This page can have any number of div where id = callout. Using xpathcount i can get the count of this, and I'll be iterating on that count and performing the href check. How can something similar be done using CSS locator?
Any help would be appreciated.
EDIT
I can click on the link using the locator css=div.callout a
, but when I try to read the href value using String str = "css=div.callout a[href]";
. I get the Error - element not found. Console description is given below.
selenium.getAttribute(str);
19:12:33.968 INFO - Command request: getAttribute[css=div.callout a[href], ] on session
19:12:33.993 INFO - Got result: ERROR: Element css=div.callout a[href not found on session
I tried to get the href attribute using xpath like this
"xpath=(//div[contains(@class, 'callout')])["+1+"]/a/@href" and it worked fine.
Please tell me what should be the corresponding CSS locator for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它应该是 -
css = div:contains(callout)
你注意到“:”而不是“.”了吗?你用过吗?
对于 CSSCount 这可能会有所帮助 -
http://www.eviltester.com/index.php/2010/03/13/a-simple-getcsscount-helper-method-for-use-with-selenium-rc/
#
另一方面,您是否看到了 51 区新硒站点的提案 - http://area51.stackexchange.com /提案/4693/硒。
#
It should be -
css = div:contains(callout)
Did you notice ":" instead of "." you used?
For CSSCount this might help -
http://www.eviltester.com/index.php/2010/03/13/a-simple-getcsscount-helper-method-for-use-with-selenium-rc/
#
On a different note, did you see proposal of new selenium site on area 51 - http://area51.stackexchange.com/proposals/4693/selenium.
#
为了阅读 sttribute,我使用了
css=div.callout a@href
并且它起作用了。问题出在属性名称周围使用方括号。To read the sttribute I used
css=div.callout a@href
and it worked. The problem was with use of square brackets around attribute name.对于问题的第一部分,将标识符锚定在超链接上:
为了根据 CSS 选择器实现 DOM 中元素的计数,这里有一个 优秀文章。
For the first part of your question, anchor your identifier on the hyperlink:
For achieving a count of elements in the DOM, based on CSS selectors, here's an excellent article.