我看着网上的例子写了小小试验程序。目标是这样的:就一个对话框,放个按钮上去。如图
运行时,一点按钮,就会启动IE,打开百度,然后自动往搜索文本框里填写内容,然后自动模拟点击“百度一下”的搜索按钮,这样网页中就会出现搜索结果了。
我代码是这样的:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace IE
{
public partial class Form1 : Form
{
private bool ie_Read = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
ie.DocumentComplete += ie_DocumentComplete;
ie.Navigate("http://www.baidu.com");
ie.Visible = true;
compWait();
mshtml.HTMLDocument doc = ie.Document;
doc.getElementById("kw").innerText = "123";// "hellow world"; //<----这句老是“值不在预期范围内”,why?
doc.getElementById("su").click();
}
private void ie_DocumentComplete(object pDisp, ref object URL)
{
ie_Read = true;
}
private void compWait()
{
while (ie_Read != true)
{
Application.DoEvents();
}
}
}
}
但上面有一句老是出错,说“值不在预期范围内”,我绞尽脑汁都没想到它怎么错了?更奇怪的是,我有两台电脑,出错是其中一台(win8, 64bit, IE11),另一台(win8,32bit, IE10)却能顺利运行!我彻底晕菜了!我不想我以后发布的程序在有的机可以运行有的机不行。到底是为什么?谁能告诉我出错哪台机是为什么错了?怎么改?万分感谢!!!!!