C#模擬瀏覽器實現(xiàn)自動操作
使用WebBrowser加載網(wǎng)頁(內(nèi)嵌)
通過WebBrowser控件打開瀏覽頁面,并操作頁面元素實現(xiàn)自動搜索功能
//是否已經(jīng)加載網(wǎng)頁標準
private bool isLoadok = false;
//首次加載網(wǎng)頁標準
private bool firstLoaded = false;
//后臺服務
private BackgroundWorker bgwork;
public WebBrowserDemo()
{
InitializeComponent();
}
/// <summary>
/// 自動窗體后加載網(wǎng)頁并獲取網(wǎng)絡元素,實現(xiàn)賦值,點擊操作
/// winform窗體上添加了一個名為webBrowser1的WebBrowser控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
this.BeginInvoke(new Action(() =>
{
bgwork = new BackgroundWorker();
bgwork.DoWork += Bgwork_DoWork;
bgwork.RunWorkerCompleted += Bgwork_RunWorkerCompleted;
string url = "https://www.so.com/";
this.webBrowser1.ScriptErrorsSuppressed = true;
this.webBrowser1.Navigate(url);
webBrowser1.DocumentCompleted += WebBrowser1_DocumentCompleted;
bgwork.RunWorkerAsync();
判斷網(wǎng)頁是否加載完成:等待不起作用,一直在加載網(wǎng)頁,使用DocumentCompleted事件
//while (this.webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
//{
// Thread.Sleep(1000);
//}
//string search_id = "input";
//string search_value = "天安門";
//string btn_id = "search-button";
//HtmlDocument doc = this.webBrowser1.Document;
//HtmlElement search = doc.GetElementById(search_id);
//search.SetAttribute("value", search_value);
//HtmlElement btn = doc.GetElementById(btn_id);
//btn.InvokeMember("click");
}));
}
/// <summary>
/// WebBrowser加載網(wǎng)頁完成事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.BeginInvoke(new Action(() =>
{
if (this.webBrowser1.IsBusy == false && webBrowser1.ReadyState == WebBrowserReadyState.Complete&& firstLoaded==false)
{
firstLoaded=true;
string search_id = "input";
string search_value = "自動化";
string btn_id = "search-button";
HtmlDocument doc = this.webBrowser1.Document;
HtmlElement search = doc.GetElementById(search_id);
search.SetAttribute("value", search_value);
HtmlElement btn = doc.GetElementById(btn_id);
btn.InvokeMember("click");
}
}));
}
/// <summary>
/// 后臺服務完成事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Bgwork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.webBrowser1.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
if(webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
isLoadok = true;
}
else
{
isLoadok = false;
}
}
/// <summary>
/// 加載網(wǎng)頁錯誤事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Error(object sender, HtmlElementErrorEventArgs e)
{
e.Handled = true;
}
/// <summary>
/// 后臺服務
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Bgwork_DoWork(object sender, DoWorkEventArgs e)
{
compWait();
}
/// <summary>
/// 后臺服務方法
/// </summary>
private void compWait()
{
while(!isLoadok)
{
Thread.Sleep(500);
}
}使用 Selenium WebDriver加載網(wǎng)頁(獨立)
Selenium WebDriver 是一個基于 Web 的自動化測試框架,可以測試在各種Web瀏覽器和各種操作系統(tǒng)上啟動的網(wǎng)頁。可以自由使用各種編程語言(例如Java、Perl、Python、Ruby、C#、PHP 和 JavaScript)編寫測試腳本。注:可以不需要下載webdriver驅(qū)動
private void btnLoad_Click(object sender, EventArgs e)
{
// 設置Chrome選項,可選
var options = new ChromeOptions();
//options.AddArgument("--headless"); // 無頭模式,不打開瀏覽器窗口
// 初始化WebDriver
IWebDriver driver = new ChromeDriver(options);
try
{
// 打開網(wǎng)頁
driver.Navigate().GoToUrl("https://account.chsi.com.cn/passport/login");
//driver.Navigate().Back();
//driver.Navigate().Refresh();
// 等待頁面加載完成,知道頁面空閑,超時10s拋出異常
//var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
//wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.BrowserIsNotBusy());
//查找用戶輸入框并輸入用戶名
driver.FindElement(By.Name("username")).SendKeys("Tom");
//查找密碼輸入框并輸入密碼
driver.FindElement(By.Name("password")).SendKeys("123456");
driver.Manage().Window.Maximize();
查找提交按鈕并點擊提交按鈕
//driver.FindElement(By.ClassName("btn_login")).Click();
// 獲取頁面源碼
string pageSource = driver.PageSource;
Console.WriteLine(pageSource);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
清理,關(guān)閉瀏覽器
//driver.Quit();
}
}到此這篇關(guān)于C#模擬瀏覽器實現(xiàn)自動操作的文章就介紹到這了,更多相關(guān)C#瀏覽器自動操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用CefSharp實現(xiàn)內(nèi)嵌網(wǎng)頁詳解
這篇文章主要介紹了C# WPF里怎么使用CefSharp嵌入一個網(wǎng)頁,并給出一個簡單示例演示C#和網(wǎng)頁(JS)的交互實現(xiàn),感興趣的小伙伴可以了解一下2023-04-04
C#開發(fā)windows服務實現(xiàn)自動從FTP服務器下載文件
這篇文章主要為大家詳細介紹了C#開發(fā)windows服務實現(xiàn)自動從FTP服務器下載文件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03

