如何使用C#ASP.Net中的元素ID將密鑰發送到網站?

asp.net c# html-agility-pack

在以下應用程序中,我使用HTML敏捷包從給定的URL中提取HTML文檔。現在我需要知道如何使用HTML文檔中的元素ID並在字段中輸入文本,最後點擊“提交”按鈕以提交表單。

protected void Button1_Click(object sender, EventArgs e)
{
    string Url = "https://something.com/login.asp"; 
    HtmlWeb web = new HtmlWeb(); 
    HtmlAgilityPack.HtmlDocument doc = web.Load(Url);
}

當前應用程序自動化Web數據輸入。

一般承認的答案

protected void Button1_Click(object sender, EventArgs e)         
{
    IWebDriver driver = 
        new InternetExplorerDriver(@"C:\.....\IEDriverServer_Win32_2.25.2"); 

    driver.Navigate().GoToUrl("https://website.com/login.asp");

    // Find the text input element by its name
    // username

    IWebElement name_ID = driver.FindElement(By.Name("name_ID"));
    name_ID.SendKeys("xyzw");

    // password
    IWebElement pwd_PW = driver.FindElement(By.Name("pwd_PW"));
    pwd_PW.SendKeys("fasdfasfdasdf");

    // submit login form
    IWebElement sSubmit = driver.FindElement(By.Name("submit"));
    submit.Submit();
    System.Threading.Thread.Sleep(5000);
    driver.Quit();
}

熱門答案

我正在做類似的事情,但我手動獲取頁面html。

// do webrequest stuff and return raw html
string html = DemoDoHttpGet(url, cookieContainer);

// I'm hitting an asp.net page so I have to repeat a bunch of values back to the server
// key is the "name" attribute of an element i want to find in the html
// i gathered these manually by watching a normal exchange with fiddler
var fields = new Dictionary<string, string>();
fields.Add(System.Web.HttpUtility.UrlDecode("__LASTFOCUS"), string.Empty);
fields.Add(System.Web.HttpUtility.UrlDecode("__EVENTTARGET"), string.Empty);
fields.Add(System.Web.HttpUtility.UrlDecode("__EVENTARGUMENT"), string.Empty);
fields.Add(System.Web.HttpUtility.UrlDecode("__VIEWSTATE"), string.Empty);
fields.Add(System.Web.HttpUtility.UrlDecode("__EVENTVALIDATION"), string.Empty);
fields.Add(System.Web.HttpUtility.UrlDecode("ctl00%24ContentPlaceHolder1%24Login1%24LoginButton"), string.Empty);

// this method searches the html for elements with the given names and updates
// the value for each item in the field collection with the value sent from the server
Scraper.GetFieldValues(fields, html);

/* looks kind of like this

        var doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(html);

        var names = new List<string>();
        foreach (var ditem in fields)
        {
            names.Add(ditem.Key);
        }

        foreach (var nitem in names)
        {
            // find items, read value

            string xpath = string.Format("//*[@name=\"{0}\"]", (nitem));
            var nodes = doc.DocumentNode.SelectNodes(xpath);

            // if node found read whatever attribute is appropriate,
            // write value back to fields collection

*/

// here i'm manually providing values for login username/password
fields.Add(System.Web.HttpUtility.UrlDecode(
    "ctl00%24ContentPlaceHolder1%24Login1%24UserName"), "my@email.aaa");
fields.Add(System.Web.HttpUtility.UrlDecode(
    "ctl00%24ContentPlaceHolder1%24Login1%24Password"), "mypassword");

// another webrequest to post back to the server

var request2 = (HttpWebRequest)WebRequest.Create(url);
request2.CookieContainer = cookieContainer;
request2.Method = "POST";
request2.ContentType = "application/x-www-form-urlencoded";

var args = new StringBuilder();
foreach (var item in fields)
{
    args.Append(System.Web.HttpUtility.UrlEncode(item.Key));
    args.Append("=");
    args.Append(System.Web.HttpUtility.UrlEncode(item.Value));
    args.Append("&");
}

using (System.IO.StreamWriter writer = 
    new System.IO.StreamWriter(request2.GetRequestStream()))
{
    writer.Write(args.ToString().TrimEnd('&'));
}

string html;
using (var response2 = (System.Net.HttpWebResponse)request2.GetResponse())
using (var rdr2 = new System.IO.StreamReader(response2.GetResponseStream()))
{
    html = rdr2.ReadToEnd();
}


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因