使用类WebBrowser打开IE并获取HtmlElement

时间:2014-06-11 17:39:51

标签: c#

我正在使用Microsoft Visual Studio 2013 Ultimate,目前正在使用Coded UI测试项目。当它用于控制台应用程序时,我收到错误"未处理的类型' System.Threading.ThreadStateException'发生在System.Windows.Forms.dll"当执行下面的第一行代码时,但是在使用我的编码UI测试项目时我没有得到它。

我的代码的最终目的是启动Web浏览器。单击某些链接,然后从浏览器中读取某些ID字段中的信息。如果有人知道这样做的更好或更简单的工作实现,我就是全力以赴。

在我目前的实施中,Internet Explorer从未启动过。

WebBrowser web = new WebBrowser(); //create object web browser
web.CreateControl();
web.Visible = true;
web.ScriptErrorsSuppressed = true;
web.Navigate("www.gogle.com"); //Shouldn't this launch IE & go to google.com?
HtmlDocument doc = web.Document;
HtmlElement el = doc.GetElementById("hplogo");//get htmlelement on the google logo
//do something with info from el.
web.Dispose(); //de-allocate

1 个答案:

答案 0 :(得分:0)

从MS KB841295,您可以将STAThread()属性添加到程序的主入口点,这应解决该特定异常。

[STAThread()]
static void Main(string[] args) {
    WebBrowser web = new WebBrowser(); //create object web browser
    //...
}
相关问题