html页面代码为:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style5
{
text-align: left;
width: 330px;
height: 123px;
}
.style6
{
width: 330px;
}
.style7
{
width: 166px;
}
.style8
{
width: 74px;
}
.style9
{
text-align: center;
width: 333px;
height: 123px;
}
.style10
{
width: 333px;
}
.style11
{
height: 123px;
}
.style12
{
width: 333px;
height: 280px;
}
.style13
{
width: 330px;
height: 280px;
}
.style14
{
height: 280px;
}
</style>
</head>
<body>
<form method="post" action="login.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNTg4NDY2NjIzZGQRa1MztZzNYRAxcKVcBH9vAeeP51Fy6RcDNFJAkezD9Q==" />
</div><div class="aspNetHidden"> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBAKq3a/nCALFppKhDQLlz6v5DALOmr/mDsLIrPNOtCeCNX8SWjZmF+oeFxzollBkynDveu/ZN3NW" />
</div>
<table class="style1" width="1280" height="690" background="images/login_back.jpg">
<tr>
<td class="style9">
<span id="Label1" style="font-size:XX-Large;font-family: 宋体, Arial, Helvetica, sans-serif">WEB系统待测登录页面</span>
</td>
<td class="style5">
</td>
<td class="style11">
</td>
</tr>
<tr>
<td class="style12">
<table class="style1">
<tr>
<td class="style7">
用户名:</td>
<td class="style8">
<input name="UserName_TextBox" type="text" id="UserName_TextBox" />
</td>
</tr>
<tr>
<td class="style7">
密 码 :</td>
<td class="style8">
<input name="PassWords_TextBox" type="text" id="PassWords_TextBox" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="Login_Botton" value="登录" id="Login_Botton" />
<span id="messageLabel" style="color:#CC0000;"></span>
</td>
</tr>
</table>
</td>
<td class="style13">
</td>
<td class="style14">
</td>
</tr>
<tr>
<td class="style10">
</td>
<td class="style6">
</td>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using SHDocVw;
using mshtml;namespace TestProgram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Launching IE ...");
Process p = Process.Start("iexplore.exe", "about:blank");
Thread.Sleep(3000);
Console.WriteLine("Attaching to IE ...");
InternetExplorer ie = null;
if (p != null)
{
SHDocVw.ShellWindows allBrowser = new ShellWindows();
if (allBrowser.Count != 0)
{
for (int i = 0; i < allBrowser.Count; i++)
{
if (allBrowser.Item(i) is InternetExplorer)
{ InternetExplorer e = (InternetExplorer)allBrowser.Item(i); if (e.HWND == (int)p.MainWindowHandle)
{
ie = e;
break;
}
}
}
}
}
else
{
Console.WriteLine("Error: Can not Launch IE");
return;
}
if (ie == null)
{
Console.WriteLine("Error: Can not attach to IE");
return;
}
Console.WriteLine("Navigating ...");
// object o = String.Empty;
object o = null;
ie.Navigate("www.baidu.com", ref o, ref o, ref o, ref o);
//ie.Navigate("www.baidu.com", ref o, ref o, ref o, ref o);
Thread.Sleep(5000);
//得到一个Text Box
Console.WriteLine("Inputing Keyword ...");
HTMLDocument doc = (HTMLDocument)ie.Document;
HTMLInputElement keyword = (HTMLInputElement)doc.getElementById("kw");
keyword.value = "天津";
Thread.Sleep(5000);
//验证
if (keyword.getAttribute("size", 0).ToString().Equals("36"))
Console.WriteLine("Validation Passed! Size is Correct");
else
Console.WriteLine("Validation Failed! Size is wrong");
//得到一个按钮
Console.WriteLine("Clicking Submit ...");
HTMLInputElement submit = (HTMLInputElement)doc.getElementById("su");
submit.click();
Thread.Sleep(4000);
//验证
if (string.Equals(doc.title.Trim(), "百度搜索_天津"))
{
Console.WriteLine("Validation Passed! Title is Corrected");
//Console.ReadLine;
}
else
Console.WriteLine("Validation Failed! Title is wrong");
if (doc.body.innerText.Contains("天津地图"))
{
Console.WriteLine("Validation Passed! Body contains your string");
Console.ReadLine();
}
else
Console.WriteLine("Validation Failed! Body do not contain");
////得到一个链接
//Console.WriteLine("Clicking Login Button ...");
//IHTMLElement userPanel = doc.getElementById("u");
//IHTMLElementCollection HyperLinks = ((IHTMLElement2)userPanel).getElementsByTagName("a");
//IHTMLElement login = (IHTMLElement)HyperLinks.item(null, 0);
//login.click();
}
提示HTMLDocument doc = (HTMLDocument)ie.Document;HTMLDocument未知接口, ie.Navigate("http://localhost/TestWebsite/login.aspx", ref o, ref o, ref o, ref o);网址改为百度是可以的,求高手给予解答
<head><title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style5
{
text-align: left;
width: 330px;
height: 123px;
}
.style6
{
width: 330px;
}
.style7
{
width: 166px;
}
.style8
{
width: 74px;
}
.style9
{
text-align: center;
width: 333px;
height: 123px;
}
.style10
{
width: 333px;
}
.style11
{
height: 123px;
}
.style12
{
width: 333px;
height: 280px;
}
.style13
{
width: 330px;
height: 280px;
}
.style14
{
height: 280px;
}
</style>
</head>
<body>
<form method="post" action="login.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNTg4NDY2NjIzZGQRa1MztZzNYRAxcKVcBH9vAeeP51Fy6RcDNFJAkezD9Q==" />
</div><div class="aspNetHidden"> <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBAKq3a/nCALFppKhDQLlz6v5DALOmr/mDsLIrPNOtCeCNX8SWjZmF+oeFxzollBkynDveu/ZN3NW" />
</div>
<table class="style1" width="1280" height="690" background="images/login_back.jpg">
<tr>
<td class="style9">
<span id="Label1" style="font-size:XX-Large;font-family: 宋体, Arial, Helvetica, sans-serif">WEB系统待测登录页面</span>
</td>
<td class="style5">
</td>
<td class="style11">
</td>
</tr>
<tr>
<td class="style12">
<table class="style1">
<tr>
<td class="style7">
用户名:</td>
<td class="style8">
<input name="UserName_TextBox" type="text" id="UserName_TextBox" />
</td>
</tr>
<tr>
<td class="style7">
密 码 :</td>
<td class="style8">
<input name="PassWords_TextBox" type="text" id="PassWords_TextBox" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="Login_Botton" value="登录" id="Login_Botton" />
<span id="messageLabel" style="color:#CC0000;"></span>
</td>
</tr>
</table>
</td>
<td class="style13">
</td>
<td class="style14">
</td>
</tr>
<tr>
<td class="style10">
</td>
<td class="style6">
</td>
<td>
</td>
</tr>
</table>
</form>
</body>
</html>C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using SHDocVw;
using mshtml;namespace TestProgram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Launching IE ...");
Process p = Process.Start("iexplore.exe", "about:blank");
Thread.Sleep(3000);
Console.WriteLine("Attaching to IE ...");
InternetExplorer ie = null;
if (p != null)
{
SHDocVw.ShellWindows allBrowser = new ShellWindows();
if (allBrowser.Count != 0)
{
for (int i = 0; i < allBrowser.Count; i++)
{
if (allBrowser.Item(i) is InternetExplorer)
{ InternetExplorer e = (InternetExplorer)allBrowser.Item(i); if (e.HWND == (int)p.MainWindowHandle)
{
ie = e;
break;
}
}
}
}
}
else
{
Console.WriteLine("Error: Can not Launch IE");
return;
}
if (ie == null)
{
Console.WriteLine("Error: Can not attach to IE");
return;
}
Console.WriteLine("Navigating ...");
// object o = String.Empty;
object o = null;
ie.Navigate("www.baidu.com", ref o, ref o, ref o, ref o);
//ie.Navigate("www.baidu.com", ref o, ref o, ref o, ref o);
Thread.Sleep(5000);
//得到一个Text Box
Console.WriteLine("Inputing Keyword ...");
HTMLDocument doc = (HTMLDocument)ie.Document;
HTMLInputElement keyword = (HTMLInputElement)doc.getElementById("kw");
keyword.value = "天津";
Thread.Sleep(5000);
//验证
if (keyword.getAttribute("size", 0).ToString().Equals("36"))
Console.WriteLine("Validation Passed! Size is Correct");
else
Console.WriteLine("Validation Failed! Size is wrong");
//得到一个按钮
Console.WriteLine("Clicking Submit ...");
HTMLInputElement submit = (HTMLInputElement)doc.getElementById("su");
submit.click();
Thread.Sleep(4000);
//验证
if (string.Equals(doc.title.Trim(), "百度搜索_天津"))
{
Console.WriteLine("Validation Passed! Title is Corrected");
//Console.ReadLine;
}
else
Console.WriteLine("Validation Failed! Title is wrong");
if (doc.body.innerText.Contains("天津地图"))
{
Console.WriteLine("Validation Passed! Body contains your string");
Console.ReadLine();
}
else
Console.WriteLine("Validation Failed! Body do not contain");
////得到一个链接
//Console.WriteLine("Clicking Login Button ...");
//IHTMLElement userPanel = doc.getElementById("u");
//IHTMLElementCollection HyperLinks = ((IHTMLElement2)userPanel).getElementsByTagName("a");
//IHTMLElement login = (IHTMLElement)HyperLinks.item(null, 0);
//login.click();
}
提示HTMLDocument doc = (HTMLDocument)ie.Document;HTMLDocument未知接口, ie.Navigate("http://localhost/TestWebsite/login.aspx", ref o, ref o, ref o, ref o);网址改为百度是可以的,求高手给予解答
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货