【问题标题】:Unable to run JavaScript in IE using Selenium无法使用 Selenium 在 IE 中运行 JavaScript
【发布时间】:2016-08-31 03:17:28
【问题描述】:

我正在尝试使用 Selenium C# 从 IE 执行 JavaScript。它在 Firefox 和 Chrome 上运行良好,但在 IE(版本 11)上运行良好。

下面是我尝试运行的示例代码:

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

【问题讨论】:

  • 不工作是什么意思??有例外吗??
  • 是 UnexpectedJavaScriptError 错误。
  • 试试string script = "arguments[0].remove();"; ((IJavaScriptExecutor) Driver.WebDriver).ExecuteScript(script, Driver.WebDriver.FindElement(By.ClassName("ITLCover"))); 告诉我..
  • 谢谢你,但脚本应该使用 FindElements 而不是 FindElement。 ((IJavaScriptExecutor) Driver.WebDriver).ExecuteScript(script, Driver.WebDriver.FindElements(By.ClassName("ITLCover")));
  • 是的,你是对的,很抱歉造成混乱

标签: c# internet-explorer selenium selenium-webdriver selenium-iedriver


【解决方案1】:

您应该尝试使用 selenium 脚本查找元素,并且需要将其作为 ExecuteScript() 的参数,如下所示:-

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

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

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

已编辑:- 如果您想传递IWebElement 的列表,并使用index 执行脚本,请尝试:-

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

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

【讨论】:

    猜你喜欢
    • 2013-08-31
    • 1970-01-01
    • 2018-11-16
    • 2017-09-17
    • 2017-09-18
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多