Reputation: 1240
I am trying to add an event to a button:
str = "a#fCoverage" // my locator
def str2 = "\$('" + str + "').addEventListener('click', function(){alert('text')});" // add event
js.exec(str2)
But I get this error:
error: org.openqa.selenium.WebDriverException: unknown error: $ is not defined
Could anyone help me please?
Upvotes: 8
Views: 21034
Reputation: 464
Use document.querySelector
inplace of $
Your code would end up
str = "a#fCoverage" // my locator
def str2 = "document.querySelector('" + str + "').addEventListener('click', function(){alert('text')});" // add event
js.exec(str2)
Upvotes: 5
Reputation: 25066
addEventListener
is a method on the document
as opposed to $
(which is usually a shorthand for jQuery).
So change $
to document
.
Upvotes: 1