Denys Medvediev
Denys Medvediev

Reputation: 1240

How fix selenium-webriver error - $ is not defined?

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

Answers (2)

Anish
Anish

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

Arran
Arran

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

Related Questions