Search code examples
xpathcss-selectorsui-automationnightwatch.jsnightwatch

How to select a specific text using CSS selector


enter image description here

I want to select the text "Auto-Publish" in Span. How can I do it with a CSS selector? with Xpath I know how to do it. I am using Nightwatch for UI automation. I want to make a generic function in the page object which will take a value as a parameter.

SelectFilterValue(value){

   .click(`//span[text()="${value}"]`)  
 }

I can't do it like this since it's an XPath and I have to specify that it's an XPath. If it was a CSS selector I could do it since I don't have to specify that it's a CSS selector. Or is there is any way that I can do it with Xpath too?


Solution

  • CSS does not have any method like text. So in HTMLDOM, it is not possible at this point of time to locate the element based on text.

    Moving further, You could do below in nightwatch.js

    .useXpath().click('//span[contains(text(), "' + desiredText+ '")]')
    

    and before calling this assign Auto-Publish to the desiredText variable.