selenium 对应 xpath 的 CSS 定位器

发布于 2024-11-01 15:08:14 字数 1431 浏览 0 评论 0原文

我正在测试的网页 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]";
selenium.getAttribute(str);
. I get the Error - element not found. Console description is given below.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愛放△進行李 2024-11-08 15:08:14

它应该是 -

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.

#

情感失落者 2024-11-08 15:08:14

为了阅读 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.

×眷恋的温暖 2024-11-08 15:08:14

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

css=a[href=http://youtube.com]

为了根据 CSS 选择器实现 DOM 中元素的计数,这里有一个 优秀文章

For the first part of your question, anchor your identifier on the hyperlink:

css=a[href=http://youtube.com]

For achieving a count of elements in the DOM, based on CSS selectors, here's an excellent article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文