开发者

CSS locator for corresponding xpath for selenium

开发者 https://www.devze.com 2023-02-25 15:21 出处:网络
The some part of the html of the webpage which I\'m testing looks like this <div id=\"twoWideCallouts\">

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.


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.

#


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消