Mldx
Mldx

Reputation: 13

Get data from the website open in the WebBrowser

I am in the same situation at the guy who asked this question. I need to get some data from a website saved as a string.

My problem here is, that the website i need to save data from, requires the user to be logged in to view the data...

So here my plan was to make the user go to the website using the WebBrowser, then login and when the user is on the right page, click a button which will automaticly save the data.

I want to use a similar method to the one used, in the top answer at the other question that i linked to in the start.

string data = doc.DocumentNode.SelectNodes("//*[@id=\"main\"]/div[3]/div/div[2]/div[1]/div[1]/div/div/div[2]/a/span[1]")[0].InnerText;

I tried doing things like this:

string data = webBrowser1.DocumentNode.SelectNodes("//*[@id=\"main\"]/div[3]/div/div[2]/div[1]/div[1]/div/div/div[2]/a/span[1]")[0].InnerText;

But you can't do "webBrowser1.DocumentNode.SelectNodes"

I also saw that the answer on the other question says, that he uses HtmlAgilityPack, but i tried to download it, and i have no idea what to do with it..

Not the best with C#, so please don't comment too complicated answers. Or at least try to make it understandable.

Thanks in advance :)

Upvotes: 0

Views: 1234

Answers (1)

r.mirzojonov
r.mirzojonov

Reputation: 1249

Here is the an example of HtmlAgilityPack usage:

public string GetData(string htmlContent)
{
      HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
      htmlDoc.OptionFixNestedTags = true;
      htmlDoc.LoadHtml(htmlContent);
      if (htmlDoc.DocumentNode != null)
      {
          string data = htmlDoc.DocumentNode.SelectNodes("//*[@id=\"main\"]/div[3]/div/div[2]/div[1]/div[1]/div/div/div[2]/a/span[1]")[0].InnerText;
          if(!string.IsNullOrEmpty(data))
             return data;
      }
      return null;
}

Edit: If you want to emulate some actions in browser I would suggest you to use Selenium instead of regular WebBrowser control. Here is the link where to download it: http://www.seleniumhq.org/ or use NuGet to download it. This is a good question on how to use it: How do I use Selenium in C#?.

Upvotes: 1

Related Questions