Nhận thấy điều đó nên bản thân luôn tìm hiểu và phát triển cho công việc hiện tại của bản thân lên một tầm cao mới, cho nên hôm nay mình quyết định chia sẽ cho mọi người một số thông tin về mã nguồn tự động
Click
, và làm những thao tác mặc định trên WebBrowser
một cách như người thật.Như nhưng code tăng lượt truy cập thật cho người dùng hiện tại.
Chúng ta bắt đầu nhé.
Trước tiên, hãy tạo một WebBrowser
và điều hướng đến đây:
//new WebBrowser, or add the control to a form instead System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser; //Hook the document completed event so we know when a page is completely loaded up wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted); //Navigate to google wb.Navigate("http://www.hung.pro.vn/"); private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { }
Bây giờ, chúng ta hãy xem cách làm việc với tài liệu của trang web để điền các giá trị chính xác để đăng nhập.
Có một vài cách để thực hiện việc này. Trước hết, nếu phần tử có "
id=***"
, chúng ta biết rằng cùng một id không thể tồn tại ở bất kỳ nơi nào khác trên cùng một trang. Vì vậy://inside of the document completed event //here's the html for the username: <input type="text" class="textbox default-value" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" /> //here's the html for the password: <input type="password" class="textbox" tabindex="102" name="vb_login_password" id="navbar_password" size="10" /> //here's the html for the button to click: <input type="submit" class="loginbutton" tabindex="104" value="Log in" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" /> //fill in the value for the username element ((WebBrowser)sender).Document.GetElementById("navbar_username").SetAttribute("value", "username"); //fill in the value for the password element ((WebBrowser)sender).Document.GetElementById("navbar_password").SetAttribute("value", "password"); //click the login element HtmlElementCollection htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName("input"); for (int i = 0; i < htmlcol.Count; i++) { if (htmlcol[i].GetAttribute("value") == "Log in") { htmlcol[i].InvokeMember("click"); } }
Tất nhiên, nếu một phần tử không có "
id=***
", chúng ta vẫn có thể truy cập vào phần tử đó. Chúng ta chỉ cần tìm ra thuộc tính/thuộc tính nào có thể sử dụng để phân biệt chúng.//inside of the document completed event //here's the html for the text to fill in to search for (without id): <input type="text" class="textbox default-value" name="vb_login_username" size="10" accesskey="u" tabindex="101" value="User Name" /> //here's the html for the password (without id: <input type="password" class="textbox" tabindex="102" name="vb_login_password" size="10" /> //here's the html for the button to click (without id): <input type="submit" class="loginbutton" tabindex="104" value="Log in" title="Enter your username and password in the boxes provided to login, or click the 'register' button to create a profile for yourself." accesskey="s" /> //get a collection of the input elements on the page HtmlElementCollection htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName("input"); //loop the input elements for(int i=0; i<htmlcol.Count; i++) { if (htmlcol[i].Name == "vb_login_username") //this is the only element with a name of "vb_login_username" { htmlcol[i].SetAttribute("value", "username"); } else if (htmlcol[i].Name == "vb_login_;password") //this is the only element with a name of "vb_login_username" { htmlcol[i].SetAttribute("value", "password"); } } //click the login element for (int i = 0; i < htmlcol.Count; i++) { if (htmlcol[i].GetAttribute("value") == "Log in") { htmlcol[i].InvokeMember("click"); } }
Bạn có thể sử dụng phần trên và sửa đổi thành bất kỳ ID nào hoặc bất kỳ phần tử nào bạn có thể xác định. Đây là cách bạn có thể đăng nhập vào các trang web và những thứ tương tự.
*Hãy nhớ rằng, hầu như bất kỳ phần tử nào cũng có thể chấp nhận nhấp chuột. Nếu phần tử bạn cần nhấp là phần tử
<img *** *** *** >
, chỉ cần lặp lại bộ sưu tập thẻ "img
" thay vì "input
" hoặc "button
" và gọi cùng một .InvokeMember("click");Có rất nhiều phần tử khác mà chúng ta cũng có thể thao tác. Các nút radio, hộp kiểm và danh sách thả xuống Tôi đã thao tác trong các ứng dụng của mình trước đây. Nhìn chung, đó là cùng một khái niệm.
Danh sách thả xuống:
//đây là danh sách thả xuống cho "
Hiển thị các chủ đề từ ...
" ở cuối diễn đàn phụ codebank mà tôi hiện đang xem//inside a document completed event //get a collection of the parent "select" elements first HtmlElementCollection htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName("select"); for(int i=0; i<htmlcol.Count; i++) { if(htmlcol[i].Name = "daysprune") //if the select element is the dropdown we need to select something for { //create another element collection for the child elements of the "select" element HtmlElementCollection htmlcolchild = htmlcol[i].Children; for(int j=0; j<htmlcolchild.Count; j++) { //<option value="1" >Last Day</option> //... //<option value="-1" selected="selected">Beginning</option> //*** Notice the selected="selected" part //if we want to select the "Last Day" instead if(htmlcolchild[j].InnerText == "Last day") //inner text gets the text between the beginning/end of the element without the attributes { htmlcolchild[j].SetAttribute("selected", "selected") //set the selected attribute for the last day, instead of the "beginning" break; } } } } //to make the changes go through, we need to click the "show threads" element //<input type="submit" class="button" value="Show Threads" /> htmlcol = ((WebBrowser)sender).Document.GetElementsByTagName("input"); for(int i=0; i<htmlcol.Count; i++) { if(htmlcol[i].GetAttribute("value") == "Show Threads") { htmlcol[i].InvokeMember("click"); break; } }
Tôi hy vọng điều này giúp ích cho mọi người trong giải trí và công việc.
Và mọi người có thể tùy biến theo khả năng của bản thân nhé. Chúc mọi người thành công.
Comments
mã nguồn này sao giống như đăng thực hiện đăng nhập lên web vậy e? nếu đăng nhập lên web thì k cần phải phức tạp đâu. Chỉ có phức tạp ở các hành động khi đăng nhập vào e nhé.
ReplyDelete