【问题标题】:ExecuteScript does not execute func described in html and which are run after page has been loadedExecuteScript 不执行 html 中描述的函数,这些函数在页面加载后运行
【发布时间】:2013-03-01 21:08:04
【问题描述】:

有 Silenium 服务器、NUnit、WebDriver。

我有带有以下代码的 C# dll:

private IWebDriver driver;

// ...

driver = new InternetExplorerDriver();

// ...

[Test]
public void test()
{
    string testURL = "file:/C:/Tests/test.html";
    driver.Navigate().GoToUrl(testURL);
    IJavaScriptExecutor js = driver as IJavaScriptExecutor;
    object resFunc = js.ExecuteScript("open_form");
    object res = js.ExecuteAsyncScript("alert('hello')");
}

文件test.html 看起来像:

<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript" src="./test.js"></script>
</head>
<body unselectable="on">
</body>
</html>

还有test.js

function open_form () {
    document.body.style.backgroundColor = "green";
}

我用控制台 NUnit 运行测试,当“hello”出现时,背景颜色没有改变。 如何运行 html 中的函数并执行某些操作?

【问题讨论】:

    标签: javascript testing selenium webdriver


    【解决方案1】:

    您缺少括号。

    制作:

        object resFunc = js.ExecuteScript("open_form()");
    

    顺便说一句,您实际上不需要使用此函数的返回值。

    【讨论】:

    • 非常感谢!这有助于运行脚本!但这仍然是解决争论的问题。因此,当我将参数传递给函数并输出警报时,参数传递为 undefined int dec = 10; string value = (string)js.ExecuteScript("return open_form()", new object[] { dec });函数 open_form (val) { alert(val);返回值; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 2023-03-06
    • 1970-01-01
    • 2015-12-21
    相关资源
    最近更新 更多