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!
contains
..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-selectorcontains
selector that jQuery has actually comes from Sizzle, not jQuery.