gpt4 book ai didi

javascript - ExecuteScript 不执行 html 中描述的函数,这些函数在页面加载后运行

转载 作者:行者123 更新时间:2023-11-28 20:56:26 25 4
gpt4 key购买 nike

有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 中的函数并执行某些操作?

最佳答案

您缺少括号。

制作它:

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

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

关于javascript - ExecuteScript 不执行 html 中描述的函数,这些函数在页面加载后运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15393662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com