6

In Selenium on Python, I'm trying to select an element using

driver = webdriver.Firefox()
driver.find_element_by_css_selector("td:contains('hello world')")

Which gives me the error:

>>>> selenium.common.exceptions.WebDriverException: Message: u'An invalid or illegal string was specified' 

I can select elements using many different CSS selectors this way, but using :contains() always seems to throw an error. NOTE: I get the same error when i try to use:

driver.find_element_by_xpath("//td[contains(text(),'hello world')]")

Please advise. Thanks!

EDIT: SOLVED!

Problem solved! Thank you so much for your help! I had two significant mistakes: 1.) I thought that :contains() was an accepted css3 selector (turns out it is not part of the current spec, which is why i couldn't select that way) 2.) The xpath selector would have been fine except that I was using a parser that assumed xpath would never have any spaces in it, so it split the arguments by spaces. Therefore, when i passed in the xpath selector

//td[contains(text(),'hello world']

the parser was truncated it at the space after 'hello' so the xpath selector looked like

//td[contains(text(),'hello

which clearly would throw an error. So i need to adjust my parsing to properly read my xpath selector.

Thank you again for all of your fast, helpful answers!

8
  • show us the html content please.. Commented Aug 28, 2013 at 14:55
  • CSS Selectors don't have anything named as contains .. Commented Aug 28, 2013 at 14:59
  • 1
    cssselect has support for :contains() pseudo-selector but it refers to an early CSS3 draft that was removed (pythonhosted.org/cssselect/#supported-selectors). I'm not aware of other projects supporting this pseuso-selector Commented Aug 28, 2013 at 15:08
  • @pault. that is jQery one - api.jquery.com/contains-selector . Selenium doesn't support that jQuery one.. Commented Aug 28, 2013 at 15:14
  • @Babai, the contains selector that jQuery has actually comes from Sizzle, not jQuery. Commented Aug 28, 2013 at 15:32

3 Answers 3

6

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()
Sign up to request clarification or add additional context in comments.

9 Comments

good catch! I hadn't made that particular mistake in my actual code though (just in my Stack Overflow question). I updated the question accordingly. Still the same error though.
@JeremyMoritz, Please show your actual code with the url you're dealing with.
@falsetru what is this br.find_element_by_xpath(u"//td[contains(text(), 'hello')]") ? what is br ?
@Babai, It was my mistake, I fixed that. br comes from my spike solution. Thank you for comment.
@falsetru Here's the error I'm getting when i use the xpath method: selenium.common.exceptions.InvalidSelectorException: Message: u'The given selector //h3[contains(text(), is either invalid or does not result in a WebElement. The following error occurred:\nInvalidSelectorError: Unable to locate an element with the xpath expression //h3[contains(text(), because of the following error:\n[Exception... "The expression is not a legal expression." code: "12" nsresult: "0x805b0033 (SyntaxError)"
|
2

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')]")

Comments

1

Instead of

driver.find_element_by_css_selector("td:contains('hello world')")

you can use this option too:

driver.find_element_by_css_selector("td*='hello world'")

Note: * in css_selector works same as contains in XPath

Reference: https://saucelabs.com/resources/articles/selenium-tips-css-selectors

2 Comments

Thank you for drawing attention to this selector! Unfortunately, it doesn't work the way you're claiming here. If you read your referenced webpage carefully, you'll see that *= (like ^= and $=) is used for selecting ATTRIBUTES of elements, NOT the INNER TEXT of those elements.
Thank you @JeremyMoritz for clearing the confusion.Yes you are right is works for attribute and not text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.