swathi_reddy
swathi_reddy

Reputation: 265

Unable to run JavaScript in IE using Selenium

I am trying to execute JavaScript from IE using Selenium C#. It's working fine on Firefox and Chrome, but not on IE (version 11).

Below is the sample code which I am trying to run:

string script = "document.getElementsByClassName('ITLCover')[0].remove();";
((IJavaScriptExecutor) Driver.WebDriver).ExecuteScript(script);

Upvotes: 1

Views: 3208

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

You should try to find element using selenium script and need to paas it as an arguments to ExecuteScript() as below :-

string script = "arguments[0].remove();"; 

IWebElement element = Driver.WebDriver.FindElement(By.ClassName("ITLCover"));

((IJavaScriptExecutor) Driver.WebDriver).ExecuteScript(script, element);

Edited :- If you want to pass list of IWebElement, and perform script using index, try as :-

int index = 0;
string script = "arguments[0][arguments[1]].remove();"; 

((IJavaScriptExecutor) Driver.WebDriver).ExecuteScript(script, Driver.WebDriver.FindElements(By.ClassName("ITLCover")), index);

Upvotes: 1

Related Questions