Reputation: 265
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
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