用于selenium的相应xpath的CSS定位器

问题描述 投票:0回答:3

我正在测试的网页的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中给出的相同,而不是错误页面。

使用Xpath我写了以下命令

//i is iterator
selenium.getAttribute("//div[contains(@class, 'callout')]["+i+"]/a/@href")

但是,这非常慢,并且对于某些链接不起作用。通过阅读本网站上的许多答案和评论我已经知道CSS loactors更快更清洁,所以我再次写作

css = div:contains(callout)

首先,我无法达到锚标签。

其次,此页面可以包含id = callout的任意数量的div。使用xpathcount我可以得到这个的计数,我将迭代该计数并执行href检查。如何使用CSS定位器完成类似的操作?

任何帮助,将不胜感激。

编辑

我可以使用定位器css=div.callout a点击链接,但是当我尝试使用String str = "css=div.callout a[href]"; selenium.getAttribute(str);读取href值时。我得到错误 - 找不到元素。控制台描述如下。

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定位器应该是什么。

selenium css-selectors selenium-rc
3个回答
0
投票

它应该是 -

css = div:contains(标注)

你注意到“:”而不是“。”你用过吗?

对于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/proposals/4693/selenium

#

0
投票

要读取属性IS使用qazxsw poo并且它有效。问题是在属性名称周围使用方括号。


0
投票

对于问题的第一部分,将标识符锚定在超链接上:

css=div.callout a@href

为了实现DOM中元素的计数,基于CSS选择器,这里是一个css=a[href=http://youtube.com]

© www.soinside.com 2019 - 2024. All rights reserved.