The first thing to say today is unrelated to the game, do you sometimes encounter, on a page repeated dozens of times simple and boring input or click work? For example, your program needs to be tested and you need to register a large number of test mailboxes. For example, your boss is a pervert, let you go to various forums to send a lot of spam posts (the most outrageous behavior, purely for example). Or, you need to regularly go to a certain page to perform some behavior, such as the clock-out system to work, etc.
If so, what I've introduced today may help you.
Of course, if you want to operate these pages are self-developed, so it doesn't matter, you are more tools to do, but I said that these are third-party pages, you can not control them, if you encounter the above situation, and you are a kick programmer, And you choose to honestly repeat these boring and time-consuming work, then you really should be kicked.
To implement these functions, you just need to program these pages and let the Web listen to you. To get started with this series of features, you need to do a few simple preparations.
1,os Environment: Windows
2,ie Browser
3, Tools: Excel or Visual Studio
First look at how to use Excel to control a Web page.
Create a new Excel and go to VBA, Standard mode, what? Don't know VBA? You can find my two teachers ask, they a surname hundred, another surname valley, eh? You said you knew that too? Then we are all our own, when will you please eat?
Such as.
I am the Japanese system, the Chinese of you against yourself to ...
To operate on IE, you first need to introduce two plugins
HTML Object Library
Microsoft Internet Controls
Open IE, you only need the following lines of code
Sub Main () Dim ie as Object Set ie = CreateObject ("Internetexplorer.application") ie. Visible = TrueEnd Sub
Like I'm going to open Baidu, that's it.
Ie. Navigate "Http://www.baidu.com"
Then you need to wait for the page to load
While IE. ReadyState <> 4 Or ie. Busy = Truedoeventswend
Well, Baidu opened, is not very simple, but you will also say, open a page to calculate what, any program can do it, OK, let's do the following to achieve automatic search.
First, we use VBA in the input box of Baidu input a few words, Baidu's input box code as follows
<input type= "text" name= "WD" id= "KW1" maxlength= "style=" WIDTH:474PX; autocomplete= "Off" >
Then we can use this ID to input the input box, as follows
Ie. document.getElementById ("KW1"). Value = "Hellow World"
"Hellow World" should have been added to the input box if you have not made any mistakes.
Then use the following VBA to click the Search button to search. Baidu's Search button code is as follows
<input type= "Submit" value= "Baidu a Bit" id= "SU1" class= "btn" onmousedown= "This.classname= ' btn btn_h '" onmouseout= " This.classname= ' BTN ' >
It's easy to see the ID, so click on it below
Ie. document.getElementById ("SU1"). Click
What's up, automatic search is done?
Of course, this is just one of the simplest examples, what if there is no ID? You can also use it below.
Ie.document.allie.document.bodyie.document.getElementsByNameie.document.getElementsByTagName
See, and JS very much like, if you do not want to check the relevant API, then you take the JS operation method to apply it
Let's look at the structure of another page, such as the following
If you want to manipulate the page's sub-page, it is also very simple
Dim Objframe as Framescollectionset objframe = Ie.document.framesDim HW as Htmlwindow2set HW = objframe (1) HW.document.all. ..
This is a sub-page with an index of 1, and of course you can loop through all the sub-pages to do the work.
The above is the first to open a page, and then the operation, if you need to operate a page that has been opened, you need this.
Dim Objshell as objectdim objIE as objectdim n as integerset Objshell = CreateObject ("Shell.Application") For n = objShell.Windows.Count to 1 step-1set objIE = objshell.windows (n-1) If objIE are nothing thenexit forend ifif Rig HT (UCase (objie.fullname),) = "IEXPLORE. EXE "Thendebug.print objIE.document.URL ' test, input urlif ObjIE.document.URL =" http://www.baidu.com "Then ' see if it is the page you want ' Find the page you want to manipulate, start processing End IfEnd Ifnextset Objshell = Nothing
That's all the more that Excel says.
The following with. NET to do, since the previous use of VBA, here I do not have to repeat the VB, in exchange for C # to achieve.
Open Visual Studio and create a new project, again, this time you need to introduce three plugins
HTML Object Library
Microsoft Internet Controls
Microsoft Shell Controls and Automation
Start IE, and turn on Baidu
Shdocvw.internetexplorer ie = new shdocvw.internetexplorer (); ie. Navigate ("http://www.baidu.com"); ie. Visible = true;
Get document
sHTML. HTMLDocument doc = ie. Document;
Similarly, enter text in the input box
Doc.getelementbyid ("KW1"). Value = "Hellow world";
Start the search.
Doc.getelementbyid ("SU1"). Click ();
How do you use C # to manipulate a browser that is already open? Look at the code below
public static Shdocvw.internetexplorer getinternetexploer (string url) {var shell = new Shell32.shell (); var windows = (shdo cvw.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;}
Well, the other way, like
Doc.bodydoc.getElementsByNamedoc.getElementsByTagName
Wait, you can apply it yourself.
Next is the frame operation, as follows
Mshtml. HTMLDocument doc2 = ie. Document;var frame = doc2.frames.item (int. Parse (configs[2]); var doc = frame. Document;doc.getelementbyid
, the operation method is basically similar.
When using. NET to manipulate the browser, I found that the operation of the same page, if the page has been refreshed or jump, and so on, the program will often be a bug, online search, found that it is not really me a person, how to avoid it?
Because the first time to use. NET to operate the page is certainly not error, must be 2 or more than 2 times before the error, so we can do two programs, call another in a program, after the call, it closed off, so each time equivalent to start a new program, it will not be a bug.
Start a new program with the following code
public static void Runsubwindow (String command) {ProcessStartInfo psInfo = new ProcessStartInfo ();p sinfo.filename = Command Psinfo.createnowindow = True;psinfo.useshellexecute = False;psinfo.redirectstandardoutput = true; Process p = process.start (psInfo); string output = P.standardoutput.readtoend ();}
You can open it by passing in the path to your program.
With the above knowledge, you are free to play, for example, you do a timed program, clock in the Office automatically, and then go to sleep for a while .... Hush!! This is definitely not what I said.
So much for today, and you're welcome to follow my blog.
Reprint Please specify: transfer from Lufy_legend's blog Http://blog.csdn.net/lufy_legend