Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 7249|回复: 11

通过VBA向网站发送数据提交!懂HTML的来指教!

[复制链接]
发表于 2019-4-29 07:55 | 显示全部楼层 |阅读模式
1学分
本帖最后由 chulia520 于 2019-4-29 08:43 编辑

想把EXCEL里面已知的数据发送到这个网站的框框里面,然后提交上去!,目前用的是IE方法。
网站入口处是一个点选框,层次太多,不知道怎么找元素怎么操作,HTML完全不懂,求老师帮助,感谢!
Sub 向网页提交数据()
    Dim sURL As String, IE As Object

    s = "1,7,9"    '输入内容,我自己已知的!

    Set IE = CreateObject("InternetExplorer.Application")
    sURL = "
https://www.ph158nb.com/login/index"    '查询内容
    With IE
    .Visible = True
        IE.Navigate sURL
        Do Until .ReadyState = 4 And .Busy = False    '信息已经返回,可以开始处理
            DoEvents
        Loop
        .Document.getElementById("gameorder-1").Value = s    '填入查询内容
        .Document.getElementById("J-submit-order").Click        '点击
    End With
    Close
    IE = Nothing
End Sub

网站源码2.jpg
阿里旺旺图片20190429073344.jpg

VBA向网站发送数据.rar

86.09 KB, 下载次数: 7

发表于 2019-4-29 08:34 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2019-4-29 08:39 | 显示全部楼层
回复

使用道具 举报

发表于 2019-4-29 08:40 | 显示全部楼层
chulia520 发表于 2019-4-29 08:39
可以打开,刷新两次就可以了

http://www.ph158nb.com/

还是打不开
回复

使用道具 举报

 楼主| 发表于 2019-4-29 08:42 | 显示全部楼层
爱疯 发表于 2019-4-29 08:40
http://www.ph158nb.com/

还是打不开

https://www.ph158nb.com/login/index
回复

使用道具 举报

发表于 2019-4-29 10:44 | 显示全部楼层
我也不大会。

但为什么你要指定这个ID,从源代码里搜索,"gameorder-1"这个ID不存在呀

回复

使用道具 举报

 楼主| 发表于 2019-4-29 10:58 | 显示全部楼层
爱疯 发表于 2019-4-29 10:44
我也不大会。

但为什么你要指定这个ID,从源代码里搜索,"gameorder-1"这个ID不存在呀

因为这个ID是根据上个点选控件检举出来的数据,上面没有数据下面就不会创建ID
回复

使用道具 举报

发表于 2019-4-29 11:03 | 显示全部楼层
我运行1楼代码,只是打开这个网址。
你具体目的是什么
回复

使用道具 举报

 楼主| 发表于 2019-4-29 11:11 | 显示全部楼层
爱疯 发表于 2019-4-29 11:03
我运行1楼代码,只是打开这个网址。
你具体目的是什么

就是免去手工去复制粘贴号码,用代码把号码加进去
回复

使用道具 举报

 楼主| 发表于 2019-4-29 11:13 | 显示全部楼层
chulia520 发表于 2019-4-29 11:11
就是免去手工去复制粘贴号码,用代码把号码加进去

我在网上找到这个资料,就是看不懂!它其实里面有我想要的,就是看不太懂!
[size=1.125]可以通过WebBrowser控件的使用实现该功能
[size=1.125]以下实例打开[size=1.125]百度[size=1.125],在输入框输入“aaa”
[size=1.125]Public Sub useie()
[size=1.125]'引用Microsoft Internet Controls
[size=1.125]Dim IE
[size=1.125]On Error Resume Next
[size=1.125]Set IE = CreateObject("InternetExplorer.application")
[size=1.125]IE.Visible = True

[size=1.125]IE.Navigate URL:="[size=1.125]https://www.baidu.com/[size=1.125]"
[size=1.125]timeie = DateAdd("s", 20, Now()) '等待20s
[size=1.125]Do While IE.Busy And Not IE.ReadyState = READYSTATE_COMPLETE
[size=1.125]DoEvents
[size=1.125]If timeie < Now() Then
[size=1.125]MsgBox “无法连接重新执行”
[size=1.125]IE.Quit
[size=1.125]Exit Sub
[size=1.125]End If
[size=1.125]Loop

[size=1.125]IE.Document.getElementById("kw").Value = "aaa"
[size=1.125]Set IE = Nothing
[size=1.125]Set ID = Nothing
[size=1.125]End Sub
[size=1.125]WebBrowser控件的使用
[size=1.125]0、常用方法
[size=1.125]Navigate(string urlString):浏览urlString表示的网址
[size=1.125]Navigate(System.Uri url):浏览url表示的网址
[size=1.125]Navigate(string urlString, string targetFrameName, byte[] postData, string additionalHeaders): 浏览urlString表示的网址,并发送postData中的消息
[size=1.125]//(通常我们登录一个网站的时候就会把用户名和密码作为postData发送出去)
[size=1.125]GoBack():后退
[size=1.125]GoForward():前进
[size=1.125]Refresh():刷新
[size=1.125]Stop():停止
[size=1.125]GoHome():浏览主页
[size=1.125]WebBrowser控件的常用属性:
[size=1.125]Document:获取当前正在浏览的文档
[size=1.125]DocumentTitle:获取当前正在浏览的网页标题
[size=1.125]StatusText:获取当前状态栏的文本
[size=1.125]Url:获取当前正在浏览的网址的Uri
[size=1.125]ReadyState:获取浏览的状态
[size=1.125]WebBrowser控件的常用事件:
[size=1.125]DocumentTitleChanged,
[size=1.125]CanGoBackChanged,
[size=1.125]CanGoForwardChanged,
[size=1.125]DocumentTitleChanged,
[size=1.125]ProgressChanged,
[size=1.125]ProgressChanged
[size=1.125]1、获取非input控件的值:
[size=1.125]webBrowser1.Document.All["控件ID"].InnerText;
[size=1.125]或webBrowser1.Document.GetElementById("控件ID").InnerText;
[size=1.125]或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
[size=1.125]2、获取input控件的值:
[size=1.125]webBrowser1.Document.All["控件ID"].GetAttribute("value");;
[size=1.125]或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
[size=1.125]3、给输入框赋值:
[size=1.125]//输入框
[size=1.125]user.InnerText = "myname";
[size=1.125]password.InnerText = "123456";
[size=1.125]webBrowser1.Document.GetElementById("password").SetAttribute("value", "Welcome123");
[size=1.125]4、下拉、复选、多选:
[size=1.125]//下拉框:
[size=1.125]secret.SetAttribute("value", "question1");
[size=1.125]//[size=1.125]复选框
[size=1.125]rememberme.SetAttribute("Checked", "True");
[size=1.125]//多选框
[size=1.125]cookietime.SetAttribute("checked", "checked");
[size=1.125]5、根据已知有ID的元素操作没有ID的元素:
[size=1.125]HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;
[size=1.125]根据Parent,FirstChild,Children[1]数组,多少层级的元素都能找到。
[size=1.125]6、获取Div或其他元素的样式:
[size=1.125]webBrowser1.Document.GetElementById("addDiv").Style;
[size=1.125]7、直接执行页面中的脚本函数,带动态参数或不带参数都行:
[size=1.125]Object[] objArray = new Object[1];
[size=1.125]objArray[0] = (Object)this.labFlightNumber.Text;
[size=1.125]webBrowser1.Document.InvokeScript("ticketbook", objArray);
[size=1.125]webBrowser1.Document.InvokeScript("return false");
[size=1.125]8、自动点击、自动提交:
[size=1.125]HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;
[size=1.125]btnAdd.InvokeMember("Click");
[size=1.125]9、自动赋值,然后点击提交按钮的时候如果出现[size=1.125]脚本错误[size=1.125]或一直加载的问题,一般都是点击事件执行过快,这时需要借助Timer控件延迟执行提交按钮事件:
[size=1.125]this.timer1.Enabled = true;
[size=1.125]this.timer1.Interval = 1000 * 2;
[size=1.125]private void timer1_Tick(object sender, EventArgs e)
[size=1.125]{
[size=1.125]this.timer1.Enabled = false;
[size=1.125]ClickBtn.InvokeMember("Click");//执行按扭操作
[size=1.125]}
[size=1.125]10、屏蔽脚本错误:
[size=1.125]将WebBrowser控件ScriptErrorsSuppressed设置为True即可
[size=1.125]11、自动点击弹出提示框:
[size=1.125]private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
[size=1.125]{
[size=1.125]//自动点击弹出确认或弹出提示
[size=1.125]IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
[size=1.125]vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认
[size=1.125]vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示
[size=1.125]}
[size=1.125]WebBrowser页面加载完毕之后,在页面中进行一些自动化操作的时候弹出框的自动点击(屏蔽)
[size=1.125]private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
[size=1.125]{
[size=1.125]//自动点击弹出确认或弹出提示
[size=1.125]IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
[size=1.125]vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //弹出确认
[size=1.125]vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//弹出提示
[size=1.125]//下面是你的执行操作代码
[size=1.125]}
[size=1.125]12、获取网页中的Iframe,并设置Iframe的src
[size=1.125]HtmlDocument docFrame = webBrowser1.Document.Window.Frames["mainFrame"].Document;
[size=1.125]或
[size=1.125]HtmlDocument docFrame = webBrowser1.Document.All.Frames["mainFrame"].Document;
[size=1.125]docFrame.All["mainFrame"].SetAttribute("src", "[size=1.125]http://www.baidu.com/[size=1.125]");
[size=1.125]13、网页中存在Iframe的时候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一样,前者是主框架的Url,后者是当前活动框口的Url。
[size=1.125]14、让控件聚焦
[size=1.125]this.webBrowser1.Select();
[size=1.125]this.webBrowser1.Focus();
[size=1.125]doc.All["TPL_password_1"].Focus();
[size=1.125]15、打开本地网页文件
[size=1.125]webBrowser1.Navigate(Application.StartupPath + @"\Test.html");
[size=1.125]16、获取元素、表单
[size=1.125]//根据Name获取元素
[size=1.125]public HtmlElement GetElement_Name(WebBrowser wb,string Name)
[size=1.125]{
[size=1.125]HtmlElement e = wb.Document.All[Name];
[size=1.125]return e;
[size=1.125]}
[size=1.125]//根据Id获取元素
[size=1.125]public HtmlElement GetElement_Id(WebBrowser wb, string id)
[size=1.125]{
[size=1.125]HtmlElement e = wb.Document.GetElementById(id);
[size=1.125]return e;
[size=1.125]}
[size=1.125]//根据Index获取元素
[size=1.125]public HtmlElement GetElement_Index(WebBrowser wb,int index)
[size=1.125]{
[size=1.125]HtmlElement e = wb.Document.All[index];
[size=1.125]return e;
[size=1.125]}
[size=1.125]//获取form表单名name,返回表单
[size=1.125]public HtmlElement GetElement_Form(WebBrowser wb,string form_name)
[size=1.125]{
[size=1.125]HtmlElement e = wb.Document.Forms[form_name];
[size=1.125]return e;
[size=1.125]}
[size=1.125]//设置元素value属性的值
[size=1.125]public void Write_value(HtmlElement e,string value)
[size=1.125]{
[size=1.125]e.SetAttribute("value", value);
[size=1.125]}
[size=1.125]//执行元素的方法,如:click,submit(需Form表单名)等
[size=1.125]public void Btn_click(HtmlElement e,string s)
[size=1.125]{
[size=1.125]e.InvokeMember(s);
[size=1.125]}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-6 01:52 , Processed in 0.332574 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表