我用VBS試了下,確實可用。與VBA的區別僅是把“DoEvents”換爲“Wscript.sleep 800”即可。
------------------------------------------------------------以上爲轉載者的註明---------------------------------------------------------------
附:看到有朋友自己弄的時候出錯了,所以我做了兩個demo,一個excel版,另一個c#版,有需要的朋友可以到文章最後下載參考一下。另外,文章改了一些疏漏。
首先今天要說的東西跟遊戲無關,你是不是有時候會遇到,在某個網頁上重複着幾十次的簡單而又無聊的錄入或點擊等工作?比如你的程序需要測試,需要註冊大量的測試郵箱。比如你的老闆是個變態,讓你去各個論壇發大量的垃圾貼子(最無恥行爲,純屬舉例)。或者,你需要定時的到某一個網頁上執行某一些行爲,比如上班下班的出勤打卡系統,等等吧,
如果有,那麼我今天所介紹的或許會對你有幫助。當然,如果你要操作的這些網頁都是自己開發的,那麼無所謂了,你在多開發個工具就行了,但是我說的上面這些都是第三方的網頁,你控制不了它們,如果遇到上上述情況,而你是一個挨踢程序員,而你又選擇老老實實的重複着這些無聊又費時的工作,那你就真該挨踢了。
要實現這些功能,你只需要用程序控制這些網頁,讓網頁聽你的就行了。要動手實現這一系列的功能,你需要做幾個簡單的準備。
1,OS環境:Windows
2,IE瀏覽器
3,工具:Excel或者Visual Studio
先來看看如何用Excel來控制一個網頁。
新建一個Excel並且進入VBA,標準模式,什麼?不知道VBA?你可以找我的兩個老師問一下,他們一個姓百,另一個姓谷,誒?你說你也認識?那大家都是自己人了,你什麼時候請吃飯啊?
如下圖。
我是日文系統,中文的你對照着自己來吧...
要對IE進行操作,首先需要引入兩個插件
HTML Object Library
Microsoft Internet Controls
如圖
打開IE,你只需要下面幾行代碼
- Sub Main()
- Dim ie As Object
- Set ie = CreateObject("InternetExplorer.Application")
- ie.Visible = True
- End Sub
比如我要打開百度,那就這樣
- ie.Navigate "http://www.baidu.com"
接着你需要等待頁面加載
- While ie.ReadyState <> 4 Or ie.Busy = True
- DoEvents
- Wend
好了,百度打開了,是不是很簡單,不過你也會說,打開個網頁算什麼,隨便一個程序都可以吧,好吧,咱們下面來實現自動搜索。
首先,咱們用VBA在百度的輸入框裏輸入幾個文字,百度的輸入框的代碼如下
- <input type="text" name="wd" id="kw1" maxlength="100" style="width:474px;" autocomplete="off">
那我們就可以通過這個id來對輸入框進行輸入,如下
- ie.Document.getElementById("kw1").value = "hellow world"
如果你上面都沒有出錯的話,"hellow world"應該已經被加入到輸入框裏了。
那麼下面用VBA來點擊搜索按鈕,進行搜索。百度的搜索按鈕代碼如下
- <input type="submit" value="百度一下" id="su1" class="btn" onmousedown="this.className='btn btn_h'" onmouseout="this.className='btn'">
看到id了吧,那就簡單了,下面這樣來點擊它
- ie.Document.getElementById("su1").click
怎麼樣,自動搜索完成了吧
當然,這只是一個最簡單的例子而已,如果沒有id怎麼辦?你也可以下下面這樣用它
- ie.document.all
- ie.document.body
- ie.document.getElementsByName
- ie.document.getElementsByTagName
看到了吧,和JS很像,如果你不願意查相關的API的話,那你就拿JS的操作方法往上面套用吧
下面來看另一個頁面的結構,比如下面這樣
- <html>
- <frameset cols="25%,50%,25%">
- <frame src="frame_a.htm" />
- <frame src="frame_b.htm" />
- <frame src="frame_c.htm" />
- </frameset>
- </html>
如果你要操作這個頁面的子頁面,也很簡單
- Dim objFRAME As FramesCollection
- Set objFRAME = ie.document.frames
- Dim HW As HTMLWindow2
- Set HW = objFRAME(1)
- HW.document.all
- ...
這個是操作索引爲1的子頁面,當然你也可以循環所有的子頁面,來做的要做的事。
上面所介紹的都是先打開一個頁面,然後再進行操作,如果需要操作一個已經打開的頁面,你需要這樣。
- Dim objShell As Object
- Dim objIE As Object
- Dim n As Integer
- Set objShell = CreateObject("Shell.Application")
- For n = objShell.Windows.Count To 1 Step -1
- Set objIE = objShell.Windows(n - 1)
- If objIE Is Nothing Then
- Exit For
- End If
- If Right(UCase(objIE.FullName), 12) = "IEXPLORE.EXE" Then
- Debug.Print objIE.document.URL '測試,輸入URL
- If objIE.document.URL = "http://www.baidu.com" Then '看看是不是你要的頁面
- '找到你要操作的頁面了,開始處理
- End If
- End If
- Next
- Set objShell = Nothing
excel就說這麼多了。
下面用.net來做,既然前面用了VBA,這裏我就不用VB再重複了,換用C#來實現。
打開Visual Studio,新建一個項目,同樣,這次你需要引入三個插件
HTML Object Library
Microsoft Internet Controls
Microsoft Shell Controls And Automation
如圖
- SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
- ie.DocumentComplete += ie_DocumentComplete;//等待頁面讀取事件
- ie.Navigate("http://www.baidu.com");
- ie.Visible = true;
等待頁面讀取你需要加一個事件
- private void ie_DocumentComplete(object pDisp, ref object URL)
- {
- ie_Read = true;
- }
- private bool ie_Read = false;
- private void compWait()
- {
- while (ie_Read != true)
- {
- Application.DoEvents();
- }
- }
獲取Document
- shtml.HTMLDocument doc = ie.Document;
同樣,在輸入框裏輸入文字
- doc.getElementById("kw1").value = "hellow world";
開始搜索。
- doc.getElementById("su1").click();
下面利用c#如何來操作已經打開的瀏覽器呢?看下面代碼
- public static SHDocVw.InternetExplorer getInternetExploer(string url)
- {
- var shell = new Shell32.Shell();
- var windows = (SHDocVw.IShellWindows)shell.Windows();
- SHDocVw.InternetExplorer ie;
- foreach (object window in windows)
- {
- ie = window as SHDocVw.InternetExplorer;
- if (ie != null &&
- string.Equals(System.IO.Path.GetFileName(ie.FullName),
- "iexplore.exe", StringComparison.CurrentCultureIgnoreCase))
- {
- if (ie.LocationURL == url)
- {
- return ie;
- }
- }
- }
- return null;
- }
好了,其他的方法如
- doc.body
- doc.getElementsByName
- doc.getElementsByTagName
等等,自己套用一下就可以了
接下來是frame操作,如下
- mshtml.HTMLDocument doc2 = ie.Document;
- var frame = doc2.frames.item(0);
- var doc = frame.Document;
- doc.getElementById
在使用.net來操作瀏覽器的時候,我發現操作同一個頁面時,如果頁面發生刷新或者跳轉等動作後,程序經常會出bug,網上搜索了一下,發現還真不是我一個人,那如何來避免呢?
因爲使用.net來第一次操作頁面的時候是肯定不會出錯的,必須是2次或2次以上纔會出現錯誤,所以我們可以做兩個程序,在一個程序中調用另一個,調用完之後,就把它關閉掉,這樣每次都相當於啓動一個新程序,就不會出bug了。
啓動一個新程序用下面代碼
- public static void runSubWindow(String command)
- {
- ProcessStartInfo psInfo = new ProcessStartInfo();
- psInfo.FileName = command;
- psInfo.CreateNoWindow = true;
- psInfo.UseShellExecute = false;
- psInfo.RedirectStandardOutput = true;
- Process p = Process.Start(psInfo);
- string output = p.StandardOutput.ReadToEnd();
- }
傳入你的程序的路徑,就可以打開它了。
有了上面的知識,你可以自由的發揮了,比如你做一個定時程序,在上班時間自動打卡,然後自己接着睡一會兒....噓!!這絕對不是我說的。
demo源碼下載
http://lufylegend.com/lufylegend_download/IE.zip
今天就介紹這麼多了,歡迎繼續關注我的博客
轉載請註明:轉自lufy_legend的博客http://blog.csdn.net/lufy_legend