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?

1
  • You are executing HTML and not Javascript code. Please read up on the JSE and how it works. Commented Nov 12, 2016 at 2:19

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

will button variable come instead of "0" in arguments? If you meant that it didn't work.
0

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

1 Comment

I updated my original script.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.