简体   繁体   中英

How to open browser for signing in and get contents in C#

I am trying to get strings from HTML using regular expression and it works on local html file. Only thing I need is to login on website using my program to get html from there.

The problem is that I tried logging in using 3 different codes without luck (I FOUND AL 3 CODES HERE). Website is HTTPS and also has no support for Internet Explorer. Don't want to use fiddler or any debugging tool. I don't care for speed, just want simple browser opening, signing in and getting html code from displayed content.

Is there any way to open chrome/mozilla/opera and transfer displayed HTML to my program? Or, if it's impossible, is there any some kind of universal way for signing in?

Is there any way to open chrome/mozilla/opera and transfer displayed HTML to my program?

You could use for example Selenium WebDriver for this. It will allow you to automate button pushing, text inputting etc. on the target web page. I wouldn't call it a "debugging tool", it's more like a testing framework. NuGet has all the packages you need:

  1. Selenium WebDriver
  2. Selenium WebDriver Support Classes

There is a really neat usage sample here :

// Initialize the Chrome Driver
using (var driver = new ChromeDriver())
{
    // Go to the home page
    driver.Navigate().GoToUrl("https://yourdomainhere.net");

    // Get the page elements
    var userNameField = driver.FindElementById("username");
    var userPasswordField = driver.FindElementById("password");
    var loginButton = driver.FindElementByXPath("//input[@value='Login']");

    // Type user name and password
    userNameField.SendKeys("admin");
    userPasswordField.SendKeys("12345");

    // and click the login button
    loginButton.Click();

    // Extract the text and save it into result.txt
    var result = driver.FindElementByXPath("//div[@id='case_login']/h3").Text;
    File.WriteAllText("result.txt", result);
}

I am trying to get strings from HTML using regular expression

I have small hunch that you.. shouldn't. You can use the driver to extract the data you want from the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM