在c#浏览器中调用成员有问题

本文关键字:成员 有问题 调用 浏览器 | 更新日期: 2023-09-27 18:04:32

    private void location()
    {
        webBrowser1.Document.GetElementById("searchInput").SetAttribute("value", "Dick's Sporting Goods");
    }
    private void pickLocation()
    {
        webBrowser1.Document.GetElementById("location_98229424").InvokeMember("click");
    }
    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        ++count;
        if (count == 1)
        {
            email = txt_email.Text;
            randomName();
            Register();
        }
        if (count == 2)
            location();
        if (count == 3)
            pickLocation();
    }

我有问题与函数"pickLocation()"。它不点击!我想不明白!有人能帮我吗?

在c#浏览器中调用成员有问题

我曾经构建了一个自动表单应用程序来自动抓取网站,导航和下载一些文件。

你指出的代码应该工作。但是,您需要更具体地说明您所针对的html文档。它可能出错的地方,实际上是试图在窗口实际加载之前访问DOM,因此在它无法定位的东西上调用方法可能会抛出异常或只是不触发。

你当然可能会有其他错误,但是在你导航到一个页面/链接添加一个像

这样的委托后尝试这个
        //create a delegate for event of documeent completed to combat multiple firings of the event
        this.webBrowser1.DocumentCompleted += delegate
        {
            if (complete)
                return;
            complete = true;
            // DocumentCompleted is fired before window.onload and body.onload
            this.webBrowser1.Document.Window.AttachEventHandler("onload", delegate
            {
                //if all awaiting threads have been fired the DOM is accessible
                System.Threading.SynchronizationContext.Current.Post(delegate
                {
                    //navigate 
                    navigate("https://website.com");
                }, null);
            });
        };
        //registering new evenhandler on document complete
        webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

这将打击你试图访问dom可能当iframe被触发等