0

I am trying to let the selenium-webdriver click a specific element on a website. That element is literally TEXT.

Now the code in javascript const spanElement = driver.findElement(By.css("span:contains('TEXT')")); throws me an error:

InvalidSelectorError: Given css selector expression "span:contains('TEXT')" is invalid: InvalidSelectorError: 'span:contains('TEXT')' is not a valid selector: "span:contains('TEXT')"

Using the Selenium IDE and css=span:contains('Strategy Tester') finds that element.

  • selenium-webdriver v3.6.0
  • geckodriver
  • firefox v60.0.2

What am I missing here?

parrott
  • 368
  • 4
  • 12

1 Answers1

0

Not sure why that works in Selenium IDE but that's not a valid CSS selector. You can do the same thing with an XPath in Selenium WebDriver.

By.xpath("//span[contains(.,'TEXT')]";
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • Thank you. That works! Is there any way to use `.By.css()` to find actual text? I read that `.By.css()` works faster than `.By.xpath()` and given that I would like to try to stick to `.By.css()` as much as possible. anyway `xpath` works for now and I use it for now. Thank you. – parrott Jun 24 '18 at 07:11
  • @r.roger I'm afraid not. CSS won't find elements by contained text. Depending on the instance, there may be ways to find the element uniquely without needing to locate it by text using CSS but that's on a case-by-case basis. – JeffC Jun 24 '18 at 19:36