0

I aimed to use Selenium Webdriver to click a button in a web page. I did it successfully on Chrome Developer Tools but I got "can't find variable" error while I was execute Javascript code:

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("$('.XbuttonName').click();");

I thought it is related with permissions because I also click the button in a healthy way with WebDriver as follows:

var button = driver.FindElement(By.ClassName("XclassName"));
button.Click();

Are there any different options to execute scripts with Selenium Driver?

tdog
  • 482
  • 3
  • 17
  • You are executing HTML and not Javascript code. Please read up on the JSE and how it works. – JeffC Nov 12 '16 at 02:19

2 Answers2

1

Actually, the element is independent from the page itself.

I use with java, can you try something like this ?

var button = driver.FindElement(By.ClassName("XclassName"));
jsE.ExecuteScript('arguments[0].click();',button);
Incepter
  • 2,711
  • 15
  • 33
  • will button variable come instead of "0" in arguments? If you meant that it didn't work. – tdog Nov 11 '16 at 22:13
0

It sounds like you haven't imported JQuery, which uses the '$' variable. See this question.

Community
  • 1
  • 1
BMac
  • 463
  • 3
  • 8