Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 260
Denys Medvediev Avatar asked Feb 23 '15 09:02

Denys Medvediev


1 Answers

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)
like image 136
Anish Avatar answered Nov 14 '22 23:11

Anish