Select an element by text content?

5,870 views
Skip to first unread message

Joel Mueller

unread,
Feb 26, 2014, 5:55:33 PM2/26/14
to nightw...@googlegroups.com
Is it possible to click an element that contains certain text? I'm trying to test an Ext JS interface that has randomly-generated ID's and a variable order of elements, so all I can count on is the text content.

It doesn't seem like CSS selectors can do such a thing, but xpath can. It seems like the "element" Selenium command might do what I want, but I'm not clear on the syntax for calling it, or what to do with the result. Any suggestions? Thanks.

Andrei Rusu

unread,
Feb 26, 2014, 6:17:43 PM2/26/14
to nightw...@googlegroups.com
Xpath support is currently not great, but I'm working on it. In the current version (v0.3.7) you can try this:

client
 
.click('xpath', '//xpath/selector', function(result) {
   
// do some stuff
 
})
 
.otherCommand();    

Joel Mueller

unread,
Feb 26, 2014, 6:48:14 PM2/26/14
to nightw...@googlegroups.com
Thanks for the reply. That worked for me, but the syntax is not awesome:

.click('xpath', "//tr[@data-recordid]/td/div/span[text()='Search Text']")

For the sake of convenience, you might want to consider supporting a jQuery-style :contains() selector even though it's not part of the CSS standard. It would allow syntax like this:

.click("tr[data-recordid]:contains('Search Text')")

I'm trying to persuade my QA team to write their own automated tests, but that xpath stuff is going to give them fits.

Andrei Rusu

unread,
Feb 27, 2014, 3:19:01 AM2/27/14
to nightw...@googlegroups.com
I agree, xpath is not very nice. I'll think about the :contains support.

Ashish Jain

unread,
Feb 5, 2016, 5:17:28 AM2/5/16
to NightwatchJs
Hi Joel,

You can get desired result using following code.

client.elements('css selector', '.myClass.having.collection.of.td', function (resultValues) {
    resultValues.value.forEach(function(element) {
        client.elementIdText(element.ELEMENT, function (result) {
             // if word found.
             if(result.value === "DesiredWord") {
                  // move the cursor on the same element and can perform click.
                  client.moveTo(element.ELEMENT, 0,0 , function () {
                         client.elementIdClick(element.ELEMENT, function (clicked) { 
                             console.log("You clicked on element a element which satisfy your if condition.");                         
                         });
                   });
             }
        }):
    });
});

Meera Madhusudhanan

unread,
Aug 26, 2019, 11:10:25 PM8/26/19
to NightwatchJs
Hi Joel,

I have tried using this code  to get click on an element that contains text but I am getting the below error.Could you please help.

"✖ TypeError: Cannot read property 'ELEMENT' of undefined"

this.api.elements("css selector", "#j-folder-menu-3>li.ui-menu-item>a", function(link_array) {

            console.log(link_array.value.length)

            

            for (var x = 0; x < link_array.value.length; x++){

              this.elementIdText(link_array.value[x].ELEMENT, function(links) {

                console.log(links.value);


                if(links.value === "xxxfolder") {

                                      // move the cursor on the same element and can perform click.

                                      this.moveTo(link_array.value[x].ELEMENT, 0,0 , function () {

                                             this.api.elementIdClick(element.ELEMENT, function (clicked) { 

                                                 console.log("You clicked on element a element which satisfy your if condition.");                         

                                             });

                                       });

                                 }

            

              });

            }

          })


Reply all
Reply to author
Forward
0 new messages