簡體   English   中英

如何在WebKitBrowser中單擊類按鈕?

[英]How do you click a class button in WebKitBrowser?

我有一個用於類按鈕的代碼:

<a class="bid-button-link button-big" href="/bid.php?scheck=fb7520ee7496e528b90117fa46dcb2ad&id=4042" title="4042"></a>

我通常可以使用常規瀏覽器執行此操作:

HtmlElementCollection elc = this.webBrowser1.Document.GetElementsByTagName("a");  
foreach (HtmlElement el in elc)  
{  
    if (el.GetAttribute("class") == "bid-button-link button-big") 
    {  
        el.InvokeMember("click");  
    }  
} 

但是由於我使用的是WebKitBrowser ,所以我不能這樣做。 我用這個代碼可以得到按鈕:

foreach (Node bidButton in webBrowser1.Document.GetElementsByTagName("a"))
{
    if (((Element)bidButton).GetAttribute("class") == "bid-button-link button-big")
    {
        //Code to click the button
    }
}

但是我沒有任何方法可以單擊按鈕,因為WebKitBrowser沒有.InvokeMember("Click")

即使它是“ Class Button也可以單擊該按鈕嗎?

如您所知,在Visual C#(WebBrowser)的常規框架中,您可以執行以下操作:

WebBrowser.Document.GetElementById("the ID").click()

但是在WebKitBrowser中,click()方法未定義,您還可以使用以下函數來模擬click方法的JavaScript代碼:

WebBrowser.StringByEvaluatingJavaScriptFromString("Document.GetElementById('the ID').click();")

希望這對您有所幫助!

編輯:

將您的代碼更改為此:

foreach (Node bidButton in webBrowser1.Document.GetElementsByTagName("a"))
{
    if (((Element)bidButton).GetAttribute("class") == "bid-button-link button-big")
    {
        bidButton.SetElement("id","Bidbutton")
        WebBrowser.StringByEvaluatingJavaScriptFromString("Document.GetElementById('BidButton').click();")
    }
}

上面的建議對我有用,但是只有在我第一次調用focus()之后,如果您遇到問題,請嘗試一下。

例如:

webKitBrowser.StringByEvaluatingJavaScriptFromString("document.getElementsByTagName('input')[7].focus();") 

webKitBrowser.StringByEvaluatingJavaScriptFromString("document.getElementsByTagName('input')[7].click();")

那bidButton.click()呢? 不知道您使用的是哪種Webkit綁定,但是在大多數工具中都可以使用click功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM