簡體   English   中英

ExecuteScript不執行html中描述的func,並且在頁面加載后運行

[英]ExecuteScript does not execute func described in html and which are run after page has been loaded

有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()");

順便說一下, 實際上您不需要使用此函數的返回值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM