[英]C# clicking a javascript alert message OK button in webbrowser control
我在C#windowsform程序中使用webbrowser控件,該程序瀏覽網站的多個頁面,然后使用網站中的某些表單進行交易。 (我試圖做到這一點httpwebrequest
和webclient
,但遇到了與餅干困難和復制一些怎樣的網站動態生成某種形式的選擇的選擇,我決定使用WebBrowser,並利用該網站的腳本-它不是我的網站)。
在最后一個步驟中,我到達一個頁面,其中包含一個表單,其中站點在提交表單時在頁面上運行驗證腳本。 如果用戶輸入了錯誤信息,則會彈出警報。
但是當我在我的程序中導航到該頁面時(在我給空字段賦值之前),我得到警報。 當我使用Chrome,Firefox或IE手動執行此操作時,不會發生這種情況。 但它發生在webbrowser中。 我可以通過提交帶有不驗證信息的表單在常規瀏覽器中復制它 - 但在Web瀏覽器中,它會在我加載頁面時發生。
我的目標是:
檢測到彈出警報已出現並成為焦點。 (警報的名稱是“來自網頁的消息。”)
單擊警報的“確定”按鈕,讓我的程序繼續輸入信息並繼續完成該過程。
這里有幾個類似於我的問題。 我找到的最有希望的帖子有以下代碼:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,
string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam,
IntPtr lParam);
private void ClickOKButton()
{
IntPtr hwnd = FindWindow("#32770", "Message from webpage");
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Button", "OK");
uint message = 0xf5;
SendMessage(hwnd, message, IntPtr.Zero, IntPtr.Zero);
}
這段代碼有點超出我的新手理解 - 我嘗試在一個新類中設置它然后實例化這個類的對象我導航到有問題的頁面然后調用ClickOKButton
方法。 沒工作。 我也嘗試在表單級別包含它,然后在程序中運行ClickOKButton
函數,我將導航到出現警報的頁面。 但它沒有用。
所以我有幾個問題:
還有另一種解決警報彈出窗口的方法嗎?
假設這段代碼有意義,我可以在調用此代碼之前運行哪種條件測試(如何檢查警報是否已出現?)
頁面在上一頁的表單的InvokeMember("submit")
命令之后加載,即出現警報時。 提交后我的代碼中的下一步是一個documentcompleted事件處理程序,然后觸發,然后完成新表單。 在完成字段之前,幾乎就像webbrowser正在提交表單一樣。 因此,我不知道在哪里插入此ClickOKButton
代碼。
在我發現的代碼中我不理解的東西中,我不理解傳遞給FindWindow的“#32770”參數。 我怎么知道這是否適合我的警報?
我寫了下面的代碼,它運行得很完美。 創建了控制台應用程序,它點擊了javasript Alert / confirm框的Ok按鈕。
using System;
using System.Runtime.InteropServices;
namespace IE_Automation
{
public class IEPoppupWindowClicker
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
private const int BM_CLICK = 0xF5;
private const uint WM_ACTIVATE = 0x6;
private const int WA_ACTIVE = 1;
public void ActivateAndClickOkButton()
{
// find dialog window with titlebar text of "Message from webpage"
var hwnd = FindWindow("#32770", "Message from webpage");
if (hwnd != IntPtr.Zero)
{
// find button on dialog window: classname = "Button", text = "OK"
var btn = FindWindowEx(hwnd, IntPtr.Zero, "Button", "OK");
if (btn != IntPtr.Zero)
{
// activate the button on dialog first or it may not acknowledge a click msg on first try
SendMessage(btn, WM_ACTIVATE, WA_ACTIVE, 0);
// send button a click message
SendMessage(btn, BM_CLICK, 0, 0);
}
else
{
//Interaction.MsgBox("button not found!");
}
}
else
{
//Interaction.MsgBox("window not found!");
}
}
}
}
嘗試使用
webBrowser.Document.ActiveElement.InvokeMember("click");
自動點擊警告框。 它對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.