0

What I need is to open a web page with a link fill some textbox with my data, click in any button and then read the data of the page in C#. For example :

Open (www.google.es) then fill the search box with "stackOverFlow", click on the search button and then read the results.

I've been looking and I think I can read the data with HttpClient but I have no clue about how to proceed with the other part.

Edit: Actually using a .net Framework console aplicattion but I can change this to an MVC app or a Winform app

F. Iván
  • 167
  • 1
  • 1
  • 11
  • 1
    Is your example remotly correct? If so, that isn't necessary - since you could just simply build the URL yourself with the search query. So, instead of going to "google.es" and enter stuff into TextBox just build the URL and navigate to the URL directly "https://www.google.es/search?q=stackOverFlow" – Rand Random Nov 14 '18 at 08:44
  • 1
    Unfortunately no, is just an example. I would like to do a generic methods. – F. Iván Nov 14 '18 at 08:53
  • Submitting a form executes a POST request. You don't need a browser to do that. You can use `HttpClient.PostAsync` to send the same request. You can use a debugging proxy like Fiddler to see what that form actually sends to the server and create an equivalent request – Panagiotis Kanavos Nov 14 '18 at 09:06
  • The *response* of PostAsync will contain the new page, which you can parse to extract the data you want. HTML parsing isn't trivial though. If you want to extract the contents of specified form fields or named elements you may be able to use regex. Otherwise you'll need a parser like HtmlAgility – Panagiotis Kanavos Nov 14 '18 at 09:09
  • Rand Random gave a good solution for Google. A generic solution (all pages) may be impossible. You can't always find the form to POST your search to, without executing the javascript ! Consider using CefSharp, which can provide a browser in a form.. then use Wolinski's answer below after the page is loaded. See: https://www.myget.org/feed/cefsharp/package/nuget/CefSharp.WinForms – Goodies Nov 14 '18 at 09:45
  • Under .net framework, if you want go simple, you could use class : WebBrowser (this method create a IE like browser simulator inside your form), then you could achieve those actions you want. Or you could use Selenium webdriver under C# (Selenium could simulate Firefox or Chorme and others). Those methods are related to use of webdriver. – Kuo-hsuan Hsu Nov 14 '18 at 10:36

2 Answers2

0

You should be able to type input by calling script:

document.getElementById("Input").value = "My value";

and then post it by calling script from question below or simulate button clink (if you are not sure that button do post)

JavaScript post request like a form submit

Simulating Button click in javascript

0

If you are trying to create a custom search and show result in your own application, you will need to find the implementation of rest API of that search provider. To get started, luckily google provides that.

follow the link to get started: https://developers.google.com/custom-search/v1/overview