问题
I want to use HtmlAgilityPack
class to login. But I don't know how.
This is what I've tried. But it's not working.
txtUserName.Text = "username";
txtPassword.Text = "password";
HtmlAgilityPack.HtmlDocument doc = new HtmlWeb().Load("about:Tabs");
doc.GetElementbyId("username").SetAttributeValue("value", txtUserName.Text);
doc.GetElementbyId("password").SetAttributeValue("value", txtPassword.Text);
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//button[@class='btn login-btn blue pull-right btn-primary']"))
{
node[0].InvokeMember("click");//wrong code
}
I cannot use WebBrowser
class.
回答1:
I found a way to login using Selenium
and I think it's the best way to do it:
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("Your Address Login");
IWebElement query = driver.FindElement(By.Id("username"));
query.SendKeys("Your Username");
query = driver.FindElement(By.Id("password"));
query.SendKeys("Your Password");
query.Submit();
download selenium for dotnet
After download add reference WebDriver.dll
and use namespace OpenQA.Selenium.Firefox;
Learn more Here
回答2:
HtmlAgilityPack can only parse html.
Use WebClient instead.
来源:https://stackoverflow.com/questions/34242048/how-to-invokemember-using-htmlagilitypack