IE编程2(.net)——通过应用程序打开google并进行搜索
2011-08-02 23:53
585 查看
/article/1451140.html
通过应用程序操作google搜索,用户输入要搜索的内容,然后在google中搜索;若开始时并没有IE实例运行,则打开一个默认的IE。
1. 加入对Microsoft Internet Controls的引用;
2. 加入对Microsoft HTML Object Library的引用;
3. 通过mshtml.IHTMLDocument2、SHDocVw.InternetExplorer、SHDocVw.ShellWindowsClass获取当前打开的google搜索页面的IE窗口句柄;
4. 根据3返回的句柄,获得当前打开的google页面的mshtml.IHTMLDocument2对象;
5. 根据4返回的IHTMLDocument2对象,获得搜索输入框和提交按钮(可查看google页面源文件,确认输入框和提交按钮的类型和名字);
6. 在搜索输入框中输入要搜索的内容,并执行提交按钮的click动作即可进行搜索;
注:本文测试在中文系统下,若在其他语言系统下,需修改StatusText的判断。
几个对象和接口的简单解释:
1. ShellWindows Object
The ShellWindows object represents a collection of the open windows that belong to the Shell. Methods are provided that can be used to control and execute commands within the Shell. There are also methods that can be used to obtain other Shell-related objects.
2. InternetExplorer Object
Controls a remote instance of Microsoft Internet Explorer through Automation.
3. IHTMLDocument2 Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface retrieves information about the document, and examines and modifies the HTML elements and text within the document.
Typically, every window object has a corresponding document object that you can retrieve by calling the QueryInterface method with the IID_IHTMLDocument or IID_IHTMLDocument2 interface identifiers. Windows that contain HTML documents always have valid document objects, but windows that contain documents in other formats might not.
4. IHTMLInputElement Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface specifies any type of input control.
5. IHTMLElement Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface provides the ability to programmatically access the properties and methods that are common to all element objects.
具体解释可参考msdn。
view plainusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestAutoSearchInGoogle
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
private void searchBtn_Click(object sender, EventArgs e)
{
if(searchText.Text.Equals(""))
{
MessageBox.Show("please input the search text", "information prompt");
return;
}
AutoSearchInGoogle.Search(searchText.Text);
}
}
public class AutoSearchInGoogle
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SetForegroundWindow(int hwnd);
#region Search
public static void Search(string searchText)
{
SHDocVw.InternetExplorer ieWnd = GetIEWndOfGoogle();
mshtml.IHTMLDocument2 ieDoc = GetIEDocOfGoogle(ref ieWnd);
System.Diagnostics.Trace.Assert(ieDoc != null);
SearchTextInGoogle(ieDoc, searchText);
//activate ie window
SetForegroundWindow(ieWnd.HWND);
}
#endregion
#region get ie window of google page
public static SHDocVw.InternetExplorer GetIEWndOfGoogle()
{
mshtml.IHTMLDocument2 ieDoc;
SHDocVw.InternetExplorer ieWnd = null;
SHDocVw.ShellWindowsClass shellWindows = new SHDocVw.ShellWindowsClass();
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
//if it is ie window
if (ie.FullName.ToUpper().IndexOf("IEXPLORE.EXE") > 0)
{
//get the document displayed
ieDoc = (mshtml.IHTMLDocument2)ie.Document;
if (ieDoc.title.ToUpper().IndexOf("GOOGLE") >= 0)
{
ieWnd = ie;
break;
}
}
}
shellWindows = null;
return ieWnd;
}
#endregion
#region get ie document of google page
public static mshtml.IHTMLDocument2 GetIEDocOfGoogle(ref SHDocVw.InternetExplorer ieWnd)
{
object missing = null;
mshtml.IHTMLDocument2 ieDoc;
if (ieWnd == null)
{
ieWnd = new SHDocVw.InternetExplorer();
ieWnd.Visible = true;
ieWnd.Navigate("http://www.google.com", ref missing, ref missing, ref missing, ref missing);
//wait for loading completed, or using DocumentComplete Event
while (ieWnd.StatusText.IndexOf("完成") == -1)
Application.DoEvents();
}
ieDoc = (mshtml.IHTMLDocument2)ieWnd.Document;
return ieDoc;
}
#endregion
#region Search the given text in google
///// <summary>
/// search the given text in google home page
/// we can see the source file of google home page to confirm the elements we need
/// the html file of google home page is as follows
///
/// <table cellpadding=0 cellspacing=0>
/// <tr valign=top>
/// <td width=25%> </td>
/// <td align=center nowrap>
/// <input name=hl type=hidden value=zh-CN>
/// <input autocomplete="off" maxlength=2048 name=q size=55 title="Google 搜索" value="">
/// <br>
/// <input name=btnG type=submit value="Google 搜索">
/// <input name=btnI type=submit value=" 手气不错 ">
/// </td>
/// ...
///// </summary>
public static void SearchTextInGoogle(mshtml.IHTMLDocument2 ieDoc, string searchText)
{
mshtml.HTMLInputElementClass input;
//set the text to be searched
foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
{
//if its tag is input and name is q(question)
if (ieElement.tagName.ToUpper().Equals("INPUT"))
{
input = ((mshtml.HTMLInputElementClass)ieElement);
if (input.name == "q")
{
input.value = searchText;
break;
}
}
}
//click the submit button to search
foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
{
//if its tag is input
if (ieElement.tagName.ToUpper().Equals("INPUT"))
{
input = (mshtml.HTMLInputElementClass)ieElement;
if (input.name == "btnG")
{
input.click();
break;
}
}
}
}
#endregion
}
}
输入DirectUIHWND,搜索结果如下:
/article/1451140.html
IE编程——通过应用程序打开google并进行搜索
通过应用程序操作google搜索,用户输入要搜索的内容,然后在google中搜索;若开始时并没有IE实例运行,则打开一个默认的IE。
1. 加入对Microsoft Internet Controls的引用;
2. 加入对Microsoft HTML Object Library的引用;
3. 通过mshtml.IHTMLDocument2、SHDocVw.InternetExplorer、SHDocVw.ShellWindowsClass获取当前打开的google搜索页面的IE窗口句柄;
4. 根据3返回的句柄,获得当前打开的google页面的mshtml.IHTMLDocument2对象;
5. 根据4返回的IHTMLDocument2对象,获得搜索输入框和提交按钮(可查看google页面源文件,确认输入框和提交按钮的类型和名字);
6. 在搜索输入框中输入要搜索的内容,并执行提交按钮的click动作即可进行搜索;
注:本文测试在中文系统下,若在其他语言系统下,需修改StatusText的判断。
几个对象和接口的简单解释:
1. ShellWindows Object
The ShellWindows object represents a collection of the open windows that belong to the Shell. Methods are provided that can be used to control and execute commands within the Shell. There are also methods that can be used to obtain other Shell-related objects.
2. InternetExplorer Object
Controls a remote instance of Microsoft Internet Explorer through Automation.
3. IHTMLDocument2 Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface retrieves information about the document, and examines and modifies the HTML elements and text within the document.
Typically, every window object has a corresponding document object that you can retrieve by calling the QueryInterface method with the IID_IHTMLDocument or IID_IHTMLDocument2 interface identifiers. Windows that contain HTML documents always have valid document objects, but windows that contain documents in other formats might not.
4. IHTMLInputElement Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface specifies any type of input control.
5. IHTMLElement Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface provides the ability to programmatically access the properties and methods that are common to all element objects.
具体解释可参考msdn。
view plainusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestAutoSearchInGoogle
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
private void searchBtn_Click(object sender, EventArgs e)
{
if(searchText.Text.Equals(""))
{
MessageBox.Show("please input the search text", "information prompt");
return;
}
AutoSearchInGoogle.Search(searchText.Text);
}
}
public class AutoSearchInGoogle
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SetForegroundWindow(int hwnd);
#region Search
public static void Search(string searchText)
{
SHDocVw.InternetExplorer ieWnd = GetIEWndOfGoogle();
mshtml.IHTMLDocument2 ieDoc = GetIEDocOfGoogle(ref ieWnd);
System.Diagnostics.Trace.Assert(ieDoc != null);
SearchTextInGoogle(ieDoc, searchText);
//activate ie window
SetForegroundWindow(ieWnd.HWND);
}
#endregion
#region get ie window of google page
public static SHDocVw.InternetExplorer GetIEWndOfGoogle()
{
mshtml.IHTMLDocument2 ieDoc;
SHDocVw.InternetExplorer ieWnd = null;
SHDocVw.ShellWindowsClass shellWindows = new SHDocVw.ShellWindowsClass();
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
//if it is ie window
if (ie.FullName.ToUpper().IndexOf("IEXPLORE.EXE") > 0)
{
//get the document displayed
ieDoc = (mshtml.IHTMLDocument2)ie.Document;
if (ieDoc.title.ToUpper().IndexOf("GOOGLE") >= 0)
{
ieWnd = ie;
break;
}
}
}
shellWindows = null;
return ieWnd;
}
#endregion
#region get ie document of google page
public static mshtml.IHTMLDocument2 GetIEDocOfGoogle(ref SHDocVw.InternetExplorer ieWnd)
{
object missing = null;
mshtml.IHTMLDocument2 ieDoc;
if (ieWnd == null)
{
ieWnd = new SHDocVw.InternetExplorer();
ieWnd.Visible = true;
ieWnd.Navigate("http://www.google.com", ref missing, ref missing, ref missing, ref missing);
//wait for loading completed, or using DocumentComplete Event
while (ieWnd.StatusText.IndexOf("完成") == -1)
Application.DoEvents();
}
ieDoc = (mshtml.IHTMLDocument2)ieWnd.Document;
return ieDoc;
}
#endregion
#region Search the given text in google
///// <summary>
/// search the given text in google home page
/// we can see the source file of google home page to confirm the elements we need
/// the html file of google home page is as follows
///
/// <table cellpadding=0 cellspacing=0>
/// <tr valign=top>
/// <td width=25%> </td>
/// <td align=center nowrap>
/// <input name=hl type=hidden value=zh-CN>
/// <input autocomplete="off" maxlength=2048 name=q size=55 title="Google 搜索" value="">
/// <br>
/// <input name=btnG type=submit value="Google 搜索">
/// <input name=btnI type=submit value=" 手气不错 ">
/// </td>
/// ...
///// </summary>
public static void SearchTextInGoogle(mshtml.IHTMLDocument2 ieDoc, string searchText)
{
mshtml.HTMLInputElementClass input;
//set the text to be searched
foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
{
//if its tag is input and name is q(question)
if (ieElement.tagName.ToUpper().Equals("INPUT"))
{
input = ((mshtml.HTMLInputElementClass)ieElement);
if (input.name == "q")
{
input.value = searchText;
break;
}
}
}
//click the submit button to search
foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
{
//if its tag is input
if (ieElement.tagName.ToUpper().Equals("INPUT"))
{
input = (mshtml.HTMLInputElementClass)ieElement;
if (input.name == "btnG")
{
input.click();
break;
}
}
}
}
#endregion
}
}
输入DirectUIHWND,搜索结果如下:
通过应用程序操作google搜索,用户输入要搜索的内容,然后在google中搜索;若开始时并没有IE实例运行,则打开一个默认的IE。
1. 加入对Microsoft Internet Controls的引用;
2. 加入对Microsoft HTML Object Library的引用;
3. 通过mshtml.IHTMLDocument2、SHDocVw.InternetExplorer、SHDocVw.ShellWindowsClass获取当前打开的google搜索页面的IE窗口句柄;
4. 根据3返回的句柄,获得当前打开的google页面的mshtml.IHTMLDocument2对象;
5. 根据4返回的IHTMLDocument2对象,获得搜索输入框和提交按钮(可查看google页面源文件,确认输入框和提交按钮的类型和名字);
6. 在搜索输入框中输入要搜索的内容,并执行提交按钮的click动作即可进行搜索;
注:本文测试在中文系统下,若在其他语言系统下,需修改StatusText的判断。
几个对象和接口的简单解释:
1. ShellWindows Object
The ShellWindows object represents a collection of the open windows that belong to the Shell. Methods are provided that can be used to control and execute commands within the Shell. There are also methods that can be used to obtain other Shell-related objects.
2. InternetExplorer Object
Controls a remote instance of Microsoft Internet Explorer through Automation.
3. IHTMLDocument2 Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface retrieves information about the document, and examines and modifies the HTML elements and text within the document.
Typically, every window object has a corresponding document object that you can retrieve by calling the QueryInterface method with the IID_IHTMLDocument or IID_IHTMLDocument2 interface identifiers. Windows that contain HTML documents always have valid document objects, but windows that contain documents in other formats might not.
4. IHTMLInputElement Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface specifies any type of input control.
5. IHTMLElement Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface provides the ability to programmatically access the properties and methods that are common to all element objects.
具体解释可参考msdn。
view plainusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestAutoSearchInGoogle
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
private void searchBtn_Click(object sender, EventArgs e)
{
if(searchText.Text.Equals(""))
{
MessageBox.Show("please input the search text", "information prompt");
return;
}
AutoSearchInGoogle.Search(searchText.Text);
}
}
public class AutoSearchInGoogle
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SetForegroundWindow(int hwnd);
#region Search
public static void Search(string searchText)
{
SHDocVw.InternetExplorer ieWnd = GetIEWndOfGoogle();
mshtml.IHTMLDocument2 ieDoc = GetIEDocOfGoogle(ref ieWnd);
System.Diagnostics.Trace.Assert(ieDoc != null);
SearchTextInGoogle(ieDoc, searchText);
//activate ie window
SetForegroundWindow(ieWnd.HWND);
}
#endregion
#region get ie window of google page
public static SHDocVw.InternetExplorer GetIEWndOfGoogle()
{
mshtml.IHTMLDocument2 ieDoc;
SHDocVw.InternetExplorer ieWnd = null;
SHDocVw.ShellWindowsClass shellWindows = new SHDocVw.ShellWindowsClass();
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
//if it is ie window
if (ie.FullName.ToUpper().IndexOf("IEXPLORE.EXE") > 0)
{
//get the document displayed
ieDoc = (mshtml.IHTMLDocument2)ie.Document;
if (ieDoc.title.ToUpper().IndexOf("GOOGLE") >= 0)
{
ieWnd = ie;
break;
}
}
}
shellWindows = null;
return ieWnd;
}
#endregion
#region get ie document of google page
public static mshtml.IHTMLDocument2 GetIEDocOfGoogle(ref SHDocVw.InternetExplorer ieWnd)
{
object missing = null;
mshtml.IHTMLDocument2 ieDoc;
if (ieWnd == null)
{
ieWnd = new SHDocVw.InternetExplorer();
ieWnd.Visible = true;
ieWnd.Navigate("http://www.google.com", ref missing, ref missing, ref missing, ref missing);
//wait for loading completed, or using DocumentComplete Event
while (ieWnd.StatusText.IndexOf("完成") == -1)
Application.DoEvents();
}
ieDoc = (mshtml.IHTMLDocument2)ieWnd.Document;
return ieDoc;
}
#endregion
#region Search the given text in google
///// <summary>
/// search the given text in google home page
/// we can see the source file of google home page to confirm the elements we need
/// the html file of google home page is as follows
///
/// <table cellpadding=0 cellspacing=0>
/// <tr valign=top>
/// <td width=25%> </td>
/// <td align=center nowrap>
/// <input name=hl type=hidden value=zh-CN>
/// <input autocomplete="off" maxlength=2048 name=q size=55 title="Google 搜索" value="">
/// <br>
/// <input name=btnG type=submit value="Google 搜索">
/// <input name=btnI type=submit value=" 手气不错 ">
/// </td>
/// ...
///// </summary>
public static void SearchTextInGoogle(mshtml.IHTMLDocument2 ieDoc, string searchText)
{
mshtml.HTMLInputElementClass input;
//set the text to be searched
foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
{
//if its tag is input and name is q(question)
if (ieElement.tagName.ToUpper().Equals("INPUT"))
{
input = ((mshtml.HTMLInputElementClass)ieElement);
if (input.name == "q")
{
input.value = searchText;
break;
}
}
}
//click the submit button to search
foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
{
//if its tag is input
if (ieElement.tagName.ToUpper().Equals("INPUT"))
{
input = (mshtml.HTMLInputElementClass)ieElement;
if (input.name == "btnG")
{
input.click();
break;
}
}
}
}
#endregion
}
}
输入DirectUIHWND,搜索结果如下:
/article/1451140.html
IE编程——通过应用程序打开google并进行搜索
通过应用程序操作google搜索,用户输入要搜索的内容,然后在google中搜索;若开始时并没有IE实例运行,则打开一个默认的IE。
1. 加入对Microsoft Internet Controls的引用;
2. 加入对Microsoft HTML Object Library的引用;
3. 通过mshtml.IHTMLDocument2、SHDocVw.InternetExplorer、SHDocVw.ShellWindowsClass获取当前打开的google搜索页面的IE窗口句柄;
4. 根据3返回的句柄,获得当前打开的google页面的mshtml.IHTMLDocument2对象;
5. 根据4返回的IHTMLDocument2对象,获得搜索输入框和提交按钮(可查看google页面源文件,确认输入框和提交按钮的类型和名字);
6. 在搜索输入框中输入要搜索的内容,并执行提交按钮的click动作即可进行搜索;
注:本文测试在中文系统下,若在其他语言系统下,需修改StatusText的判断。
几个对象和接口的简单解释:
1. ShellWindows Object
The ShellWindows object represents a collection of the open windows that belong to the Shell. Methods are provided that can be used to control and execute commands within the Shell. There are also methods that can be used to obtain other Shell-related objects.
2. InternetExplorer Object
Controls a remote instance of Microsoft Internet Explorer through Automation.
3. IHTMLDocument2 Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface retrieves information about the document, and examines and modifies the HTML elements and text within the document.
Typically, every window object has a corresponding document object that you can retrieve by calling the QueryInterface method with the IID_IHTMLDocument or IID_IHTMLDocument2 interface identifiers. Windows that contain HTML documents always have valid document objects, but windows that contain documents in other formats might not.
4. IHTMLInputElement Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface specifies any type of input control.
5. IHTMLElement Interface
Stock Implementation: mshtml.dll
Inherits from: IDispatch interface
Header and IDL files: Mshtml.h, Mshtml.idl
This interface provides the ability to programmatically access the properties and methods that are common to all element objects.
具体解释可参考msdn。
view plainusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestAutoSearchInGoogle
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void exitBtn_Click(object sender, EventArgs e)
{
this.Close();
}
private void searchBtn_Click(object sender, EventArgs e)
{
if(searchText.Text.Equals(""))
{
MessageBox.Show("please input the search text", "information prompt");
return;
}
AutoSearchInGoogle.Search(searchText.Text);
}
}
public class AutoSearchInGoogle
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int SetForegroundWindow(int hwnd);
#region Search
public static void Search(string searchText)
{
SHDocVw.InternetExplorer ieWnd = GetIEWndOfGoogle();
mshtml.IHTMLDocument2 ieDoc = GetIEDocOfGoogle(ref ieWnd);
System.Diagnostics.Trace.Assert(ieDoc != null);
SearchTextInGoogle(ieDoc, searchText);
//activate ie window
SetForegroundWindow(ieWnd.HWND);
}
#endregion
#region get ie window of google page
public static SHDocVw.InternetExplorer GetIEWndOfGoogle()
{
mshtml.IHTMLDocument2 ieDoc;
SHDocVw.InternetExplorer ieWnd = null;
SHDocVw.ShellWindowsClass shellWindows = new SHDocVw.ShellWindowsClass();
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
//if it is ie window
if (ie.FullName.ToUpper().IndexOf("IEXPLORE.EXE") > 0)
{
//get the document displayed
ieDoc = (mshtml.IHTMLDocument2)ie.Document;
if (ieDoc.title.ToUpper().IndexOf("GOOGLE") >= 0)
{
ieWnd = ie;
break;
}
}
}
shellWindows = null;
return ieWnd;
}
#endregion
#region get ie document of google page
public static mshtml.IHTMLDocument2 GetIEDocOfGoogle(ref SHDocVw.InternetExplorer ieWnd)
{
object missing = null;
mshtml.IHTMLDocument2 ieDoc;
if (ieWnd == null)
{
ieWnd = new SHDocVw.InternetExplorer();
ieWnd.Visible = true;
ieWnd.Navigate("http://www.google.com", ref missing, ref missing, ref missing, ref missing);
//wait for loading completed, or using DocumentComplete Event
while (ieWnd.StatusText.IndexOf("完成") == -1)
Application.DoEvents();
}
ieDoc = (mshtml.IHTMLDocument2)ieWnd.Document;
return ieDoc;
}
#endregion
#region Search the given text in google
///// <summary>
/// search the given text in google home page
/// we can see the source file of google home page to confirm the elements we need
/// the html file of google home page is as follows
///
/// <table cellpadding=0 cellspacing=0>
/// <tr valign=top>
/// <td width=25%> </td>
/// <td align=center nowrap>
/// <input name=hl type=hidden value=zh-CN>
/// <input autocomplete="off" maxlength=2048 name=q size=55 title="Google 搜索" value="">
/// <br>
/// <input name=btnG type=submit value="Google 搜索">
/// <input name=btnI type=submit value=" 手气不错 ">
/// </td>
/// ...
///// </summary>
public static void SearchTextInGoogle(mshtml.IHTMLDocument2 ieDoc, string searchText)
{
mshtml.HTMLInputElementClass input;
//set the text to be searched
foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
{
//if its tag is input and name is q(question)
if (ieElement.tagName.ToUpper().Equals("INPUT"))
{
input = ((mshtml.HTMLInputElementClass)ieElement);
if (input.name == "q")
{
input.value = searchText;
break;
}
}
}
//click the submit button to search
foreach (mshtml.IHTMLElement ieElement in ieDoc.all)
{
//if its tag is input
if (ieElement.tagName.ToUpper().Equals("INPUT"))
{
input = (mshtml.HTMLInputElementClass)ieElement;
if (input.name == "btnG")
{
input.click();
break;
}
}
}
}
#endregion
}
}
输入DirectUIHWND,搜索结果如下:
相关文章推荐
- IE编程2(.net)——通过应用程序打开google并进行搜索
- 通过使用客户端证书调用 Web 服务以便在 ASP.NET Web 应用程序中进行身份验证
- [翻译]ASP.NET MVC 3 开发的20个秘诀(十)[20 Recipes for Programming MVC 3]:通过关键字进行列表搜索
- ADO.NET数据库编程 利用应用程序配置文件进行数据的读取
- 讨论asp.net通过机器cookie仿百度(google)实现搜索input搜索提示弹出框自己主动
- 利用VB.Net 通过Windows Sockets (Winsock)以及多线程编程进行桌面电脑与Pocket PC间的双向通信
- Android 编程日记 如何通过包名打开一个应用程序
- 浅谈asp.net通过本机cookie仿百度(google)实现搜索input框自动弹出搜索提示
- IE编程3(.net)——应用程序操作IE6
- WebDriver实现自动化打开IE中的google网页并实现搜索
- VC.NET界面编程中关于的ToolBar(工具栏)的编程应用(二)2008/07/01 19:22 上回说到给工具栏上添加IE风格的下拉菜单按钮,我们通过设置工具栏按钮的风格已经完成了下拉菜单按钮的添加,现在我们准备为下拉菜单按钮中响应下拉箭头部分的实现
- 自动打开IE,google并按天气预报搜索
- 浅谈asp.net通过本机cookie仿百度(google)实现搜索input框自己主动弹出搜索提示
- WebDriver实现自动化打开IE中的google网页并实现搜索
- Android2.2调用Google Map api进行位置搜索
- 驱动开发之 应用程序通过设备接口打开设备
- 解决Chorme使用google搜索结果无法打开的问题
- Android 通过WebService进行网络编程,使用工具类轻松实现
- 在IE中像迅雷那样通过右击链接打开程序
- 怎样用好Google进行搜索