简体   繁体   中英

DocumentCompleted firing only once

I have a WebBrowser inside a form and I want to do some automation with it. I click a button inside a windows form that commands the Navigate method of the WebBrowser to a certain page. Then I automatically click a link after DocumentCompleted has fired but after that I want to also click a Button that exists in the new page that appeared by clicking the link. It seems DocumentCompleted fires only when I click the button in the windows form not when I automatically click the a link inside the webpage.

    void BtnTestClick(object sender, EventArgs e)
    {
        webBrowser1.Navigate(@"https://play.google.com/apps/");
        webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);     
    }

    public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

        var webBrowser = sender as WebBrowser;
        //webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;

        // test to see if we're on fist CONFIRM page then go forward by clicking
        var links = webBrowser1.Document.GetElementsByTagName("a");
        foreach (HtmlElement link in links)
        {
            if (link.InnerText == "Proceed anyway")
            {
                link.InvokeMember("click");
            }
        }   // this works

    webBrowser1.Document.GetElementById("gwt-uid-126").InvokeMember("click"); 
    }

After the link.InvokeMember("click"); a new page loads in the webbrowser that has a button which I also want to click ( gwt-uid-126 )

But it doesn't get clicked.

I've also tried:

var elements = webBrowser1.Document.GetElementsByTagName("button");
foreach (HtmlElement file in elements)
{
    if (file.GetAttribute("class") == "GKYRWGTDNX GKYRWGTDLY")
    {
        file.Focus();
        file.InvokeMember("click");

    }
}   

With no luck!

From what I see, second click doesn't work because the document is not completely loaded and second click is invoked.

You will have to add another if-else block that handled second document load.

Edit1: I was on phone when I answered this, so couldn't provide any snippet. Following is the change that you can do WebBrowser_DocumentCompleted method.

var links = webBrowser1.Document.GetElementsByTagName("a");
foreach (HtmlElement link in links)
{
    if (link.InnerText == "Proceed anyway")
    {
        link.InvokeMember("click");
    }
}
// following is for the page that is loaded on click of link.
var gwt_uid_126 = webBrowser1.Document.GetElementById("gwt-uid-126");
if(gwt_uid_126 != null)
{
    gwt_uid_126.InvokeMember("click");
}

You might want to check if the WebBrowser_DocumentCompleted method is actually being called on second page load. This might be the reason why second click is not registering.

move this part of code in the Constructor or Form_Load:

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);

try this instead of using WebBrowserDocumnetCompletedEventHandler:

void btnTestClick(object sender, EventArgs e)
{
    webBrowser1.Navigate(@"https://www.google.com/");

    while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
        continue;

    var webBrowser = sender as WebBrowser;
    //webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;

    // test to see if we're on fist CONFIRM page then go forward by clicking
    var links = webBrowser1.Document.GetElementsByTagName("a");
    foreach (HtmlElement link in links)
    {
        if (link.InnerText == "Proceed anyway")
        {
            link.InvokeMember("click");
        }
    }   // this works

    webBrowser1.Document.GetElementById("gwt-uid-126").InvokeMember("click"); 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM