-1
<span _ngcontent-c77="" class="status-label success" tooltipcontainer="body" tooltipheader="Error" tooltipmode="click" tooltipposition="top" data-test-id="status"> online </span>

In xpath i can find this element by

//*[@data-test-id="status" and text()=" online "]

But i can't understand how to find this element with containing text ' online ' using css selector. Help me with this, please.

Oladuwek
  • 43
  • 1
  • 6

4 Answers4

2

You can't do it with css_selector, xpath is the only way to locate by text. There is find_element_by_link_text, but only for <a> tags, and without additional attributes.

JeffC
  • 22,180
  • 5
  • 32
  • 55
Guy
  • 46,488
  • 10
  • 44
  • 88
1

You cannot because Selenium supports CSS2, and contains() is CSS3. You can find detailed information here

Sers
  • 12,047
  • 2
  • 12
  • 31
0

You won't be able to locate an element by using the innerText i.e. contains('string'). You can find a detailed discussion in selenium.common.exceptions.InvalidSelectorException with "span:contains('string')".

To locate an element you can use either of the following Locator Strategies:

  • CSS_SELECTOR 1:

    span.status-label.success[tooltipcontainer='body'][tooltipheader='Error']
    
  • CSS_SELECTOR 2:

    span[data-test-id='status'][tooltipcontainer='body'][tooltipheader='Error']
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

This is not possible.

But i think you can use the "data-"

Copy content from you span to data: data-text="online"

For example:

<span _ngcontent-c77="" data-text="online" class="status-label success" tooltipcontainer="body" tooltipheader="Error" tooltipmode="click" tooltipposition="top" data-test-id="status"> online </span>

And CSS:

/* Select every cell containing "Online" */
span[data-text*="online"] {
  color: green;
}