如何自动填写网页元素

AllCHN 2010-05-18 09:51:24
在VB.NET中用WebBrowser打开一个网页,网页里面有一个名为 login_frame 的 iframe 内嵌网页
在 login_frame 的网页里面有一个 form, 如何自动填写其中的输入区域,比如自动填写 "username" (<input type=text name=username>)
...全文
197 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
AllCHN 2010-05-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wuyq11 的回复:]
webbrower跨域
http://topic.csdn.net/u/20091122/22/521caf78-eea6-4b1a-b904-c58a2fe50215.html
[/Quote]

把源码贴在这:
MessageBox.Show(webBrowser1.Document.Window.Frames["main"].Document.Body.InnerHtml);
HTMLDocument doc = (HTMLDocument)webBrowser1.Document.DomDocument;
object j;
for (int i = 0; i < doc.parentWindow.frames.length; i++)
{
j = i;
HTMLWindow2Class frame = doc.parentWindow.frames.item(ref j) as HTMLWindow2Class;
if (frame.name == "main")
{
MessageBox.Show(frame.document.title);
}
}
IHTMLDocument2 doc = (IHTMLDocument2)webBrowser1.Document.Window.Frames["main"].Document.DomDocument;

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
string url = webBrowser1.Document.Window.Frames["main"].Document.ActiveElement.GetAttribute("src");
}

VB.Net 我这样用(myweb 是 WebBrowser 控件):
If mywb.Document.Window.Frames("login_frame").Document.GetElementById("username").Name <> "" Then
mywb.Document.Window.Frames("login_frame").Document.GetElementById("username").SetAttribute("value", "123")
mywb.Document.Window.Frames("login_frame").Document.GetElementById("password").SetAttribute("value", "456")
End If

谢谢!
AllCHN 2010-05-18
  • 打赏
  • 举报
回复
简单方法肯定是没用的

webBrowser.Navigate(JavaScriptCode);
应该是导航到一个网址,怎么会出现Javascript?

WebBrowser1.Document.getElementByID("ID").Value = "dddddddddddddddddd"
这个方法对没有嵌套的网页也许有用,用了 iframe 完全没用的

好像我的描述应该比较清楚的
mngzilin 2010-05-18
  • 打赏
  • 举报
回复
WebBrowser wb;
HtmlDocument doc= wb.Document.GetElementById("login_frame").Document;
HtmlElement name=doc.GetElementById("username");
name.SetAttribute("value", "123");
HtmlElement pwd = doc.GetElementById("userpwd");
pwd.SetAttribute("value", "123");
doc.GetElementById("submitBtn").InvokeMember("click");
皇城龙三 2010-05-18
  • 打赏
  • 举报
回复
'these next three functions are used to enumerate the Windows handles of all Windows sited on the specified parent...
Function GetChildWindowHandles(ByVal ParentWindowHandle As IntPtr) As ArrayList

Dim b As Boolean
Dim ptrChild As IntPtr
Dim clsRet As New ArrayList

'get first child handle...
ptrChild = GetChildWindowHandle(ParentWindowHandle)

Do Until ptrChild.Equals(IntPtr.Zero)
'add to collection of handles...
clsRet.Add(ptrChild)
'get next child...
ptrChild = GetNextWindowHandle(ptrChild)
Loop

'return...
Return clsRet

End Function

Function GetChildWindowHandle(ByVal ParentWindowHandle As IntPtr) As IntPtr
Return GetWindow(ParentWindowHandle, GW_CHILD)
End Function

Function GetNextWindowHandle(ByVal CurrentWindowhandle As IntPtr) As IntPtr
Return GetWindow(CurrentWindowhandle, GW_HWNDNEXT)
End Function

'this function returns the text of the window, used so that we can confirm that we have the right dialog window...
Function GetWindowText(ByVal WindowHandle As IntPtr) As String

Dim ptrRet As IntPtr
Dim ptrLength As IntPtr

'get length for buffer...
ptrLength = SendMessageA(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)

'create buffer for return value...
Dim sb As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)

'get window text...
ptrRet = SendMessageString(WindowHandle, WM_GETTEXT, ptrLength.ToInt32 + 1, sb)

'get return value...
Return sb.ToString

End Function
linkinwhite 2010-05-18
  • 打赏
  • 举报
回复
WebBrowser1.Document.getElementByID("ID").Value = "dddddddddddddddddd"
Justin-Liu 2010-05-18
  • 打赏
  • 举报
回复
webBrowser.Navigate(JavaScriptCode);
Justin-Liu 2010-05-18
  • 打赏
  • 举报
回复
用JavaScript

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧