Browser emulation programmatically in C # /. Net

We would like to automate certain tasks on the website, for example, “log in” the user, perform some functions, read the history of their accounts, etc.

We tried to simulate this with the usual POST / GET, but the problem is that, for example, to “log in”, the site uses javascript code to make an AJAX call, and also generates some random tokens.

Is it possible to literally emulate a web browser? For instance:

  • Visit www. [test-website] .com '
  • Fill these DOM elements
    • DOM username "username" is populated with "testuser"
    • DOM password "password" is filled with "testpass"
    • Click on the DOM button item 'btnSubmit'
  • View account history
    • Read HTML (so that we can analyze information about each individual element of the story)
  • ...

The above can be translated as follows:

var browser = new Browser(); var pageHomepage = browser.Load("www.test-domain.com"); pageHomepage.DOM.GetField("username").SetValue("testUser"); pageHomepage.DOM.GetField("password").SetValue("testPass"); pageHomepage.DOM.GetField("btnSubmit").Click(); var pageAccountHistory = browser.Load("www.test-domain.com/account-history/"); var html = pageAccountHistory.GetHtml(); var historyItems = parseHistoryItems(html); 
+4
source share
4 answers

You can use, for example, Selenium in C #. There is a good tutorial: Testing data using selenium (webdriver) in C # .

+2
source

I would suggest creating an instance of the WebBrowser control in code and doing all your work with this instance, but never showing it in any form. I have done this several times and it works very well. The only drawback is that it uses Internet Explorer; -)

+1
source

Try JMeter, which is also good for automating web requests, and is also quite popularly used to test website performance.

0
source

Or just try System.Windows.Forms.WebBrowser, for example:

 this.webBrowser1.Navigate("http://games.powernet.com.ru/login"); while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) System.Windows.Forms.Application.DoEvents(); HtmlDocument doc = webBrowser1.Document; HtmlElement elem1 = doc.GetElementById("login"); elem1.Focus(); elem1.InnerText = "login"; HtmlElement elem2 = doc.GetElementById("pass"); elem2.Focus(); elem2.InnerText = "pass"; 
0
source

Source: https://habr.com/ru/post/1487306/


All Articles