简体   繁体   中英

how to click separate webpage element in web browser control

I used web browser control to click elements in webpage.In first page i want to click submit button and after that web browser navigate to 2nd page. Then i wanted to click an element in 2nd page.

then i tried following code to do it. But after running the project it gave null exception.

        {
            webBrowser1.Document.GetElementById("userid").SetAttribute("value", textBox1.Text);
            webBrowser1.Document.GetElementById("pass").SetAttribute("value", textBox2.Text);
            timer1.Start();

            //webBrowser1.Document.GetElementById("sgnBt").InvokeMember("click");



        }

        private void Form4_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("https://www.ebay.com/ws/eBayISAPI.dll?SignIn&lgout=");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            HtmlElementCollection classButton = webBrowser1.Document.All;
            foreach (HtmlElement element in classButton)
            {
                if (element.GetAttribute("className") == "btn ipb btn-prim sgnBtn")
                {
                    element.InvokeMember("click");
                }
            }

            timer1.Stop();
            webBrowser1.Navigate("https://www.ebay.com/itm/362735358056");
          Thread.Sleep(5000);

            webBrowser1.Document.GetElementById("atl_btn_lnk").InvokeMember("click");


        }

ID = atl_btn_lnk is in 2nd webpage.. But when starting the project that atl_btn_lnk is not available . so it gives me an error. plz how to fix this issue.

You problem is that timer1_Tick is trying to handle both the sign in page and item page ( https://www.ebay.com/itm/362735358056 ) and your code will always try to handle both pages.

With the limitations of the code you've provided, you could create another timer that handles the item page. Start this timer after you successfully navigated to the item page.

private void timer1_Tick(object sender, EventArgs e)
{
    HtmlElementCollection classButton = webBrowser1.Document.All;
    foreach (HtmlElement element in classButton)
    {
        if (element.GetAttribute("className") == "btn ipb btn-prim sgnBtn")
        {
            element.InvokeMember("click");
        }
    }

    timer1.Stop(); // stop the first timer
    webBrowser1.Navigate("https://www.ebay.com/itm/362735358056");
    timer2.Stop(); // start the item page timer.
}

private void timer2_Tick(object sender, EventArgs e)
{   
    // item page.
    try
    {
        var elem = webBrowser1.Document.GetElementById("atl_btn_lnk");
        if(elem!=null) // check if element exist.
        {
            elem.InvokeMember("click");
            timer2.Stop();
        }
    }
    catch(Exception e)
    {

    }
}

Tip: It's always a good practice to check the return value of GetElementById and try to click only if the item exists.

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