Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to click on the 2nd element with the same class using Nightwatch.js

I have been trying out a lot of different ways to click on a particular element on the browser using Nightwatch.js (nth-child, nth-of-type, etc), and so far I am not able to find the correct element. I am trying to click on the 2nd "More" button on the screen.

enter image description here

The HTML looks like this. Both of the "More" buttons have the exact same class, and are nested within a div that has a key difference in the class, in that one is called discover-teams and the second is nested within a div that has a class of discover-athletes. If I try something like this, I end up clicking on one of the follow buttons on the image

.click('.discover-athletes div:nth-child(3) button')

enter image description here

If anyone knows of the best way to do this I would greatly appreciate it. So far I am coming up short. Much obliged

like image 345
Andy Pohl Avatar asked Apr 12 '16 18:04

Andy Pohl


People also ask

What is Nightwatch JSON?

Nightwatch. js is a Selenium wrapper that provides several out-of-the-box commands and assertions for performing various operations on the page. The commands and assertions are clean and straightforward, enabling you to develop tests very quickly using Javascript (specifically, Node. js).

What is Nightwatch framework?

Nightwatch. js framework is a Selenium-based test automation framework, written in Node. js and uses the W3C WebDriver API (formerly Selenium WebDriver). It works by communicating over a restful HTTP API with a WebDriver server (such as ChromeDriver or Selenium Server).


1 Answers

I see that the page has two ".discover-athletes" so the selector for 2nd button should be :

'test' : function(browser){
       const 2ndSelector = 'div[class="discover-athletes"]:nth-child(2) > div:nth-child(3) > button';
       browser.click(2ndSelector);
          }

You need symbol ">" to make selector more accurate.

Edit:there is only 1 ".discover-athletes",but it make no difference.

        'test' : function(browser){
           const 2ndSelector = 'div[class="discover-athletes"] > div:nth-child(3) > button';
           browser.waitForElementVisible(2ndSelector,5000,false,function(result){
                 browser.click(2ndSelector);
                          });              
                  }
like image 155
Bao Tran Avatar answered Oct 18 '22 20:10

Bao Tran