Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Internet Explorer实例中运行JavaScript函数_C#_Internet Explorer_Automation - Fatal编程技术网

C# 在Internet Explorer实例中运行JavaScript函数

C# 在Internet Explorer实例中运行JavaScript函数,c#,internet-explorer,automation,C#,Internet Explorer,Automation,我正在使用 SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer() 控制/自动化Internet Explorer的实例。在某些页面上,我希望运行JavaScript函数(init())。似乎最好的方法是使用HtmlDocument的InvokeScript方法,我一直在尝试以下方法,但没有成功: void ie_DocumentComplete(object pDisp, ref object URL) { System.

我正在使用

SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer()
控制/自动化Internet Explorer的实例。在某些页面上,我希望运行JavaScript函数(
init()
)。似乎最好的方法是使用
HtmlDocument
InvokeScript
方法,我一直在尝试以下方法,但没有成功:

void ie_DocumentComplete(object pDisp, ref object URL)
{
  System.Windows.Forms.HtmlDocument doc = ie.Document;
  doc.InvokeScript("init");
}
因为
doc
为空,所以失败。我似乎无法从
ie.Document
获取
System.Windows.Forms.HtmlDocument
。除了尝试上述方法外,我还尝试了:

System.Windows.Forms.HtmlDocument doc2 = (System.Windows.Forms.HtmlDocument)ie.Document;

我有什么想法可以让这个工作-或甚至更好的方式来运行页面上的脚本

谢谢

编辑

运行JavaScript函数的另一种方法似乎是:

SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer()
mshtml.HTMLDocument doc = ie.Document;
mshtml.IHTMLWindow2 win = doc.parentWindow as mshtml.IHTMLWindow2;
win.execScript("init();", "javascript");
但是这条线

mshtml.IHTMLWindow2 win = doc.parentWindow as mshtml.IHTMLWindow2;

抛出一个错误,表明它是无效强制转换(
InvalidCastException
)-即使IntelliSense(和MSDN)说
doc.parentWindow
是一个
IHTMLWindow2
。有什么想法吗?(我还确保在运行该代码之前已完全加载页面)

SHDocVw.InternetExplorer.Document的类型为mshtmlHTMLDocumentClass,因此您需要引用Microsoft.mshtml

mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)ie.Document;
ie对象还必须导航到某个地方,文档才能有值。比如

object test = new object();
ie.Navigate("c:\\tmp\\test1.html", ref test, ref test, ref test, ref test);
总初始值:

SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
object test = new object();
ie.Navigate("c:\\tmp\\test1.html", ref test, ref test, ref test, ref test);
mshtml.HTMLDocumentClass doc = (mshtml.HTMLDocumentClass)ie.Document;

这个问题与线程有关——我在STA问题上浪费了太多时间,你可能认为我现在已经学会了:)

无论如何,我找到了一种方法来获取我发布的第二段代码,在IE窗口中运行javascript函数!代码如下:

this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
                {

                        mshtml.HTMLDocument doc = ie.Document;

                        mshtml.IHTMLWindow2 win = doc.parentWindow as IHTMLWindow2;
                        win.execScript("init();", "javascript");


                }));

希望它能帮助别人

您必须在STA线程中访问document.parentWindow。这可能会帮助您:

  private WebBrowser _webBrowser; //initialize this somewhere

  private void ExecuteJavaScript()
  {
     Thread aThread = new Thread(ExecuteJavaScriptWorker);
     aThread.SetApartmentState(ApartmentState.STA);
     aThread.Start(); 
  }

  private void ExecuteJavaScriptWorker()
  {
      HTMLDocument _document = _webBrowser.Document;
      _document.parentWindow.execScript("alert('Arbitrary javascript code')", "javascript");
  }

这是一个如何获取某个页面的文档的示例。它与上面所示的示例非常接近,但差别很小(但很重要)——我使用的是Navigate2方法——这一方法工作正常

public static mshtml.HTMLDocument NavigateTo(String anUrl) {
  object locEmpty = 0;
  object locUrl = anUrl;
  SHDocVw.InternetExplorer _ie = new SHDocVw.InternetExplorer();
  _ie.Visible = true;
  _ie.Navigate2(locUrl, ref locEmpty, ref locEmpty, ref locEmpty, ref locEmpty);
  return(_ie.Document);
}   
此示例适用于IE在常规(非模式)窗口中打开的所有页面。 对于模式窗口(或模式对话框),此示例不起作用。

您可以简单地执行以下操作:

ie.Navigate("javascript:" + jsScript);

ie是你的internetexplorer的实例

谢谢你的回答,但是我很难让这条线路正常工作。它给出了以下编译器错误:“不能嵌入互操作类型‘mshtml.HTMLDocumentClass’。请改用适用的接口。”有什么想法吗?(这一行的意思是“mshtml.HTMLDocumentClass doc=(mshtml.HTMLDocumentClass)ie.Document;”您在引用中添加了对Microsoft.mshtml的引用吗?(不是互操作)它在.net引用列表下。是的,我有引用。我能够控制浏览器窗口并打开新页面,只是不运行脚本。不过我使用的是C#4.0,这与它有什么关系吗?我只是问,因为它在COM中改变了一些东西(比如能够呼叫ie.导航而不必填写额外的参数)。再次感谢,伙计!这与C#4.0有点关系-嵌入mshtml时不能使用HTMLDocumentClass。但是,我将嵌入值设置为false,这样我就可以运行上面的代码,而我无法将HTMLDocumentClass转换为Windows.Forms.HTMLDocument,这样我就可以使用InvokeScript().有什么想法吗?如果你能具体解释一下你是如何解决线程问题的,你的答案可能会更有帮助。:-)顺便说一句:正如我在一些页面上看到的,Dispatcher.Invoke来自WPF。你有普通C#项目的解决方案吗?非常有限!在你可以执行的代码方面!
ie.Navigate("javascript:" + jsScript);