It is not possible.
Selenium CSS Selectors do not support locating elements based on their text content.
See this or this or other similar questions about that.
It is not possible.
Selenium CSS Selectors do not support locating elements based on their text content.
See this or this or other similar questions about that.
EDIT
Try this:
driver.find_element_by_css_selector("div:contains('Safety Stock')")
or this:
driver.find_element_by_css_selector("div[data-o9title='Safety Stock']")
java - CSS Locator with contains() InvalidSelectorException using Selenium WebDriver - Stack Overflow
selenium - Does CSS Selector have contains text similar to xpath? - Stack Overflow
selenium - A way to match on text using CSS locators - Software Quality Assurance & Testing Stack Exchange
Css locator with Innertext with Selenium Webdriver - Stack Overflow
The main problem is at this line:
driver.findElement(By.cssSelector("a:contains('About Google')"));
css doesn't maintain contains() for Selenium WD - See here.
For using contains() you have to use Xpath.
With Xpath your locator will be:
//a[contains(text(), 'About Google')]
and for driver it will be as:
driver.findElement(By.xpath("//a[contains(text(), 'About Google')]"));
For finding links with Selenium you can use:
driver.findElement(By.linkText("your link name here"));
It is limitation of CSS selectors compare to Xpath:
- you can't take parent element with CSS selectors (Xpath has XPath axes)
- you can't use contains (it is only XPath privilege).
BTW
For processing Xpath locators from page you able to use extension for Firefox browser:
FirePath
Xpath Checker
CssSelector does not work in scripting but it works in selenium IDE.
It's also not good to work on sites like gmail.
No, css_selector for Selenium still doesn't supports :contains("text") method.
You can find a couple of detailed discussions in:
- selenium.common.exceptions.InvalidSelectorException with "span:contains('string')"
- Finding link using text in CSS Selector is not working
The contains() pseudo-class may not work with browsers that don't natively support CSS selectors. Also, it has been deprecated from CSS3 specification.
so the alternative is using innerText
WebElement cell = driver.findElement(By.cssSelector("td[innerText='Item 1']"));
or textContent
WebElement cell = driver.findElement(By.cssSelector("td[textContent='Item 1']"));
css=a[text='Log Out'] or a[innertext='Log Out']
Can you please try this one out?
Or if that doesn't work and you still don't want to use xpath because it's slow, you can always try: link=Log Out. That's still better then xpath.
EDIT:
So i found a possible solution for you mate. If you are trying to find an exact String you could always use Regular expression like this:
css=div:contains("^ab$")
Just replace div with a and there you go. This will find ONLY AB in whatever text div it looks for. OFC if you have more then one links with text AB (which is a bad thing :P ) then it will find them all..
Try this and see if it helps. :)
This is a nice place for a few CSS selectors.
http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/
Thought it might be useful for people following this thread.