Skip to content

Instantly share code, notes, and snippets.

@sdhjl2000
Created December 11, 2012 07:17
Show Gist options
  • Save sdhjl2000/4256533 to your computer and use it in GitHub Desktop.
Save sdhjl2000/4256533 to your computer and use it in GitHub Desktop.
NHtmlUnit
using System;
using System.Linq;
using NHtmlUnit;
using NHtmlUnit.Html;
using NUnit.Framework;
using HtmlAgilityPack;
using SimpleBrowser;
namespace APSPTest
{
[TestFixture]
public class WebClientTests
{
[Test]
public void DoWikipediaSearch()
{
string username = "admin_xbyt";
string password = "password";
string loginurl = "http://oa.xbyt.sinopec.com/names.nsf?login&username=" + username + "&password=" +
password+"&redirectto=";
string dburl = "http://oa.xbyt.sinopec.com/xbsyj/persontasks.nsf/dbview?openform&view=vwgwdbshow_" + username + "&count=100";
string serverurl = "oa.xbyt.sinopec.com/";
CookieAwareWebClient client = new CookieAwareWebClient();
string resString = client.DownloadString(loginurl);
resString = client.DownloadString(dburl);
//Console.WriteLine(resString);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(resString);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//textarea[@name='viewHTMLValue']");
HtmlNode tbnode = node.DescendantNodes().FirstOrDefault(desc => desc.Name.Equals("table", StringComparison.OrdinalIgnoreCase));
//Console.WriteLine(tbnode.InnerHtml);
if (tbnode != null)
{
foreach (HtmlNode row in tbnode.DescendantNodes().Where(desc => desc.Name.Equals("tr", StringComparison.OrdinalIgnoreCase) && desc.DescendantNodes().Any(child => child.Name.Equals("td", StringComparison.OrdinalIgnoreCase))))
{
var alink = row.DescendantNodes().FirstOrDefault(
desc => desc.Name.Equals("a", StringComparison.OrdinalIgnoreCase));
if (alink != null)
{
var nnode = HtmlNode.CreateNode(HtmlEntity.DeEntitize(alink.InnerHtml));
Console.WriteLine(nnode.InnerText);
string docurl = nnode.Attributes["href"].Value.Replace("DNS:xbsyj/", serverurl);
if (docurl.Contains("sw"))
{
ProcessPage(client, loginurl + docurl);
//ProcessPageSimple(client, loginurl + docurl);
}
}
}
}
}
private void ProcessPageSimple(CookieAwareWebClient client, string logurl)
{
var browser = new Browser();
browser.Navigate(logurl);
// click the login link and click it
// browser.Log("First we need to log in, so browse to the login page, fill in the login details and submit the form.");
var swtitle = browser.Find("fldSubject", FindBy.Name);
if (!swtitle.Exists)
browser.Log("Can't find the login link! Perhaps the site is down for maintenance?");
else
{
Console.WriteLine(swtitle);
}
}
private void ProcessPage(CookieAwareWebClient client, string docurl)
{
//var resString= client.DownloadString(docurl);
//HtmlDocument doc = new HtmlDocument();
//doc.LoadHtml(resString);
//HtmlNode node = doc.DocumentNode.SelectSingleNode("//input[@name='fldFwbh']");
//if (node != null) { Console.WriteLine(node.InnerText); }
// Note that this test works against live wikipedia.org and requires an active Internet connection
WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7)
{
JavaScriptEnabled = true,
ActiveXNative = true,
CssEnabled = false,
ThrowExceptionOnScriptError=true,
ThrowExceptionOnFailingStatusCode=true,
};
// HtmlPage page=(NHtmlUnit.Html.HtmlPage)com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(new com.gargoylesoftware.htmlunit.StringWebResponse(resString, new java.net.URL("http://www.baidu.com")), (com.gargoylesoftware.htmlunit.WebWindow)webClient.CurrentWindow);
HtmlPage page = webClient.GetHtmlPage(docurl);
HtmlInput queryInputElement = page.GetElementByName<HtmlInput>("fldSubject");
Console.WriteLine(queryInputElement.AsXml());
//HtmlSubmitInput submitButton2 =
// page.HtmlElementDescendants
// .OfType<HtmlSubmitInput>()
// .First(e => e.NameAttribute == "go");
//submitButton2.Click();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment