-3

I have this xpath generated from firebug but I need to use a CSS selector in my code. Just take the xpath as the code. So my code has 7 div tags where 7 div tag has 2 div tag and then <a class="btn btn">. So how do write my CSS Selector for this?

html/body/div[7]/div[2]/a[1] : - Class = "btn btn"

Here the <a class = "btn btn"> tag has an attribute class which is unique on the web page.

  • here <a class = "btn btn"> tag has an attribute class which is unique on the web page – Monika Apr 15 '15 at 3:40
  • Xpath and Css selector both are diff. methods to find element. I think you are mixing those. – Helping Hands Apr 15 '15 at 3:41
  • I have this xpath generated from firebug but need to use CSS selector in my code. Just take the xpath as the code. so my code has 7 div tags where 7 div tag has 2 div tag and then <a class="btn btn">. So how do write my CSS Selector for this – Monika Apr 15 '15 at 3:44
  • 1
    Just open your site in chrome , open firebug , inspect element and do right click , you will get option called "Copy Csspath" , this will give you exact css selector. – Helping Hands Apr 15 '15 at 3:46
  • 1
    Hi Monika, welcome to sqa.stackexchange! About your question: on this site, when you ask a question you really need to show what you have tried so far and say why you think it isn't working; you shouldn't just ask for a solution to your problem without having tried it yourself first. If you don't know how css selectors work at all, then there are many tutorials where you can find out; Alan Richardson's book Selenium Simplified is very good. – Vince Bowdren Apr 15 '15 at 9:07
2

This answer might not seem very useful right now, but tools which automatically generate xpath or css for an element pretty much never do a good job of creating a selector which is not brittle.

I strongly advise that you spend a day or two learning in depth either xpath, or css (Webdriver supports either, css selectors tend to be a bit quicker) as it is not complicated, and not rely on automatically generating selectors, they really cause a lot of problems and very very weak tests that break quickly.

For example, if we add in another div somewhere in the middle of the other 7 you already have before your element, the xpath you have there breaks entirely. Xpath in the right hands can be well written and quite robust.

Quick answer : btn is the class on your link. It looks like it's been decorated with the same class twice (smells wrong to me).. The css selector would be a.btn if the btn class is unique on all a tags like you said.

Not the answer you're looking for? Browse other questions tagged or ask your own question.