1

I looked into this question for issues with CSS selectors. The accepted answer is move away from nightwatch.js altogether. I'm hoping this isn't still the case but am having problems.

I've upgraded to the latest available nightwatch.js v0.9.14 but can't get it to find elements on screen that I can find using jQuery.

Using the selector below (hierarchy necessary as it's within a jqGrid):

.click("#MedicalHistoryAmendment_sheet tbody tr:eq(1) > td:eq(4) > select")

I get:

ERROR: Unable to locate element: "#MedicalHistoryAmendment_sheet tbody tr:eq(1) > td:eq(4) > select
 using: css selector

Also trying the selector method from this question (would prefer not to do this as the ids are dynamic):

 .click('select[id="null_z01rsnload_inst_ref"]')

Fails with:

ERROR: Unable to locate element: "select[id="null_z01rsnload_inst_ref"]" using: css selector

Using jQuery in the console:

$("#MedicalHistoryAmendment_sheet tbody tr:eq(1) > td:eq(4) > select")

Returns:

[select#null_z01rsnload_inst_ref.editable, prevObject: init(1), context: document, selector: "#MedicalHistoryAmendment_sheet tbody tr:eq(1) > td:eq(4) > select"]

I've had similar issues to this before (particularly with select's and their options) but the method mentioned in the answer by 79E09796 has worked for me previously.

This is on Chrome (our primary target, we're not doing automated testing for other platforms at the moment) using chromedriver.exe version 2.28. It is not inside an iframe.

Adding "--verbose" to the nightwatch command line gave me:

INFO Request: POST /wd/hub/session/50a51b48-bce9-425d-a132-2d0407c8ac21/elements
 - data:  {"using":"css selector","value":"#MedicalHistoryAmendment_sheet tbody tr:eq(1) > td:eq(7) select"}
 - headers:  {"Content-Type":"application/json; charset=utf-8","Content-Length":98}
ERROR Response 500 POST /wd/hub/session/50a51b48-bce9-425d-a132-2d0407c8ac21/elements (1019ms) { sessionId: '50a51b48-bce9-425d-a132-2d0407c8ac21',
  status: 32,
  value:
   { message: 'invalid selector: An invalid or illegal selector was specified\n
..<snip>..

Any further ideas would be much appreciated!

Community
  • 1
  • 1
Stuart Brock
  • 3,596
  • 1
  • 22
  • 21
  • Can you try running Nightwatch with --verbose? If it's doing anything wrong, you might see it mangle the selector string in the POST to Selenium. Otherwise it's just printing the error that's returned (which you'll also see in the verbose output.) Maybe .waitForElementPresent() first? – Jonathan Ross Apr 19 '17 at 19:42
  • A few notes: you should be able to use querySelector in some cases. Also, what browser has this problem, or is it all browsers? Also, is the element nested inside an iframe? You need to explicitly select the iframe before you can manipulate it's HTML elements. – QualiT Apr 19 '17 at 21:44
  • Added chromedriver info, doing more debugging at the moment. – Stuart Brock Apr 20 '17 at 09:57
  • 1
    :eq() is a jQuery selector, and non-standard. Unless nightwatch.js explicitly supports it (in which case, it's also a non-standard implementation), you won't be able to use it there. – BoltClock Apr 20 '17 at 09:59
  • @BoltClock can you post that as an answer, it seems to be correct! I'll do a further writeup too. – Stuart Brock Apr 20 '17 at 10:21

1 Answers1

0

As @BoltClock said the eq() operator isn't a valid CSS selector, it's jQuery specific. I changed to use nth-child() instead.

So:

.assert.elementPresent("#MedicalHistoryAmendment_sheet tbody tr:eq(1) > td:eq(7) select")

Needs to be:

.assert.elementPresent("#MedicalHistoryAmendment_sheet tbody tr:nth-child(2) > td:nth-child(8) select")

To change, you need to increase the index by 1 and also bear in mind it may grab extra elements depending on the structure​.

Other selectors in this category that are jQuery only are gt(), lt()

Thanks for all the help everyone.

Community
  • 1
  • 1
Stuart Brock
  • 3,596
  • 1
  • 22
  • 21