When trying to click on an element with dynamic text in my app, I encounter a syntax error. Normally, to select an element by its text, I use the following format:
.useXpath().click("//*[contains(text(), 'some text')]")
But for elements with dynamic names, I attempt the following approach:
.useXpath().click("//*[contains(text(), "${name}]")
The variable "name" holds the name of the newly created account. However, when I run my tests, I face the following issue:
.useXpath().click("//*[contains(text(), "${name}"])")
^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: missing ) after argument list
I have tried different combinations of single and double quotes but haven't been successful. I also explored string concatenation as suggested in this discussion: (Passing a variable into an XPath expression in CasperJS), which led me to modify my code like this:
var selector = "\'//*[contains(text(), \'" + name + "\')]\'";
.useXpath().click(selector)
Unfortunately, when I implement this method, I receive the following error message:
ERROR: Unable to locate element: "'//*[contains(text(), '7ayhopt3ijhcwuo')]'" using: xpath
The text '7ayhopt3ijhcwuo' is the correct target, but it cannot be found. Interestingly, if I hard code the value like so:
.useXpath().click("//*[contains(text(), '7ayhopt3ijhcwuo')]")
nightwatch successfully clicks on the desired text without any issues.
If anyone can provide guidance on how to resolve these methods, I would greatly appreciate it! My environment uses node v7.8.0