我正在尝试将JavaScript代码注入网站并运行此功能:
String js = "var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
"script.text = \"function myFunction() { " +
"return 'test method';" +
"}\";" +
"(document.head || document.body || document.documentElement).appendChild(script);";
chromeBrowser.ExecuteScriptAsync(js);
chromeBrowser.EvaluateScriptAsync("myFunction();");
这是一个返回文本的简单代码。
当我尝试注入它并致电myFunction
时,我收到以下错误消息:
Uncaught ReferenceError: myFunction is not defined @ about:blank:1:0
答案 0 :(得分:1)
由于您使用的是Async
方法,可能是myFunction
尚未定义但很快就会出现吗?
尝试一下:
chromeBrowser.EvaluateScriptAsync("setTimeout(myFunction, 100);");