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
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.
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.
selenium - Does CSS Selector have contains text similar to xpath? - Stack Overflow
xpath - Selenium Python: find_element_by_css_selector() using :contains() - Stack Overflow
CSS selector :contains doesn't work with Selenium 2.0a7
Python Selenium get element by CSS matching text - Stack Overflow
Videos
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']"));
Replace find_element_by_xpath_selector with find_element_by_xpath (There is no find_element_by_xpath_selector method):
driver = webdriver.Firefox()
...
driver = driver.find_element_by_xpath(u"//td[contains(text(), 'hello')]")
COMPLETE EXAMPLE
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://python.org')
link = driver.find_element_by_xpath(u'//a[contains(text(), "Download")]')
link.click()
You are doing wrong way. You need to create first a driver object as below:
driver = webdriver.Firefox()
Then navigate to the required url as below:
driver.get("http://www.python.org")
Then on that driver object need to call the required method. There is no contains CSS Selectors. You can go ahead by this method find_element_by_xpath as below :
elem = driver.find_element_by_xpath("//td[contains(text(), 'hello')]")
As you said it's supported in xpath:
This would be a solution with an xpath using contains and text()
driver.find_element_by_xpath('//div[@class="button-face-caption" and contains(text(),"Text I want to find")]')
The xpath being:
//div[@class="button-face-caption" and contains(text(),"Text I want to find")]
For css, look here: https://sqa.stackexchange.com/q/362/34209 which should allow us to use:
div:contains('Text I want to find')
Which would lead us to
driver.find_element_by_css("div:contains('Text I want to find')")
However this comes with a BIG caveat:
:contains() is not part of the current CSS3 specification so it will not work on all browsers, only ones that implemented it before it was pulled. (see w3.org/TR/css3-selectors)
As workaround you can create your own function
def find_by_css(selector, text=''):
return [element for element in driver.find_elements_by_css_selector(selector) if text in element.text][0]
Then you can call it as
find_by_css('div.button-face-caption') # search only by CSS-selector
or
find_by_css('div.button-face-caption', 'Text I want to find 2') # search by CSS + text