Writing cssselector expression for webDriver using

2019-08-19 02:37发布

问题:

I am a newbie to css, html, etc and thus cssselectors. I have been messing around with selenium webdriver and am trying to .click() the following button:

<button title="" class="btn addWidgButt bt-block" href="" role="button" type="button"     context="UNIQUE_THING">
<span class="btn-text">UNIQUE THING</span></button>

According to http://www.w3.org/TR/css3-selectors/#selectors shouldn't the following selector work? (Rule is E[foo="bar"] for element E

By.cssSelector("button title[context=UNIQUE_THING]")
By.cssSelector("btn.addWidgButt.bt-block[context=UNIQUE_THING]")
By.cssSelector("span[btn-text=UNIQUE THING]")

I think the last one is my best bet, as span is an element and btn-text is an attribute value. Any help is appreciated, thanks.

回答1:

One thing I noticed is that all of your selectors in [] are missing single quotes.

By.cssSelector("button[context='UNIQUE_THING']")
By.cssSelector(".btn.addWidgButt.bt-block[context='UNIQUE_THING']")
By.cssSelector("span.btn-text")

Your second selector is missing the leading .

Your last selector will not work, as you cannot match text with CSS Selectors. You can match a class with ., or id with #.

For more information, this is an excellent reference: http://www.w3schools.com/cssref/css_selectors.asp