Loading web pages using WebBrowser (inline)
Open the browsing page through the WebBrowser control and operate the page elements to achieve automatic search function
//Is the web page standard already loaded private bool isLoadok = false; //First loading of web page standards private bool firstLoaded = false; //Backend Service private BackgroundWorker bgwork; public WebBrowserDemo() { InitializeComponent(); } /// <summary> /// After the automatic form, load the web page and obtain network elements to achieve assignment, click operation /// A WebBrowser control named webBrowser1 was added to the winform form /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { (new Action(() => { bgwork = new BackgroundWorker(); += Bgwork_DoWork; += Bgwork_RunWorkerCompleted; string url = "/"; this. = true; this.(url); += WebBrowser1_DocumentCompleted; (); Determine whether the web page is loading:Waiting doesn't work,Loading the web page,useDocumentCompletedevent //while (this. || != ) //{ // (1000); //} //string search_id = "input"; //string search_value = "*"; //string btn_id = "search-button"; //HtmlDocument doc = this.; //HtmlElement search = (search_id); //("value", search_value); //HtmlElement btn = (btn_id); //("click"); })); } /// <summary> /// WebBrowser loads the web page to complete the event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { (new Action(() => { if (this. == false && == && firstLoaded==false) { firstLoaded=true; string search_id = "input"; string search_value = "automation"; string btn_id = "search-button"; HtmlDocument doc = this.; HtmlElement search = (search_id); ("value", search_value); HtmlElement btn = (btn_id); ("click"); } })); } /// <summary> /// Backend service completion event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Bgwork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { this. += new HtmlElementErrorEventHandler(Window_Error); if( == ) { isLoadok = true; } else { isLoadok = false; } } /// <summary> /// Loading web page error event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Window_Error(object sender, HtmlElementErrorEventArgs e) { = true; } /// <summary> /// Backend service /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Bgwork_DoWork(object sender, DoWorkEventArgs e) { compWait(); } /// <summary> /// Backend service method /// </summary> private void compWait() { while(!isLoadok) { (500); } }
Loading web pages using Selenium WebDriver (standalone)
Selenium WebDriver is a web-based automated testing framework that can test web pages launched on various web browsers and operating systems. Test scripts are free to write in various programming languages such as Java, Perl, Python, Ruby, C#, PHP, and JavaScript. Note: You can download the webdriver driver without needing to
private void btnLoad_Click(object sender, EventArgs e) { // Set Chrome options, optional var options = new ChromeOptions(); //("--headless"); // Headless mode, does not open the browser window // Initialize WebDriver IWebDriver driver = new ChromeDriver(options); try { // Open the web page ().GoToUrl("/passport/login"); //().Back(); //().Refresh(); // Wait for the page to load, know that the page is idle, and an exception is thrown after 10 seconds //var wait = new WebDriverWait(driver, (10)); //(()); //Find the user input box and enter the user name (("username")).SendKeys("Tom"); //Find the password input box and enter the password (("password")).SendKeys("123456"); ().(); Find the Submit button and click the Submit button //(("btn_login")).Click(); // Get the page source code string pageSource = ; (pageSource); } catch(Exception ex) { (); } finally { Clean up,Close the browser //(); } }
This is the article about C# simulated browser automatic operation. For more information about C# automatic operation, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!