none
When using webBrowser find element by XPath c# RRS feed

  • Question

  • When I use webBrowser is there anyway to go to a certain page and find like a button, using the XPath. Or is there any other way that I could go to a website and find an element using the XPath, other than using a webbrowser?
    WebBrowser web = new WebBrowser();
    flowLayoutPanel1.Controls.Add(web);
    //System.Threading.Thread.Sleep(2500);
    web.Width = 590;
    web.Navigate("https://google.com")


    Saturday, June 22, 2019 9:58 PM

All replies

  • When I use webBrowser is there anyway to go to a certain page and find like a button, using the XPath. Or is there any other way that I could go to a website and find an element using the XPath, other than using a webbrowser?
    WebBrowser web = new WebBrowser();
    flowLayoutPanel1.Controls.Add(web);
    //System.Threading.Thread.Sleep(2500);
    web.Width = 590;
    web.Navigate("https://google.com")


    No, not with Xpath….

    https://www.codeproject.com/Articles/547451/WebBrowser-Element-Events-and-Values

    There should be a Getattribute like there is a SetAttribute for browser.Document.GetElementById 

    Sunday, June 23, 2019 2:58 AM
  • Hi Forzen,

    Thank you for posting here.

    Based on your description, you want to use webbrowser find element.

    I suggest that you could try HtmlElement, I make a simple code for your reference.

    Code:

     WebBrowser web = new WebBrowser();
            private void Form1_Load(object sender, EventArgs e)
            {
    
                this.Controls.Add(web);
                web.Width = 590;
                web.Navigate("https://google.com");
            }
    
            private void Button1_Click(object sender, EventArgs e)
            {
                var buttonControls = (from HtmlElement element in web.Document.GetElementsByTagName("input")select element).ToList();
                HtmlElement submitButton =
                    buttonControls.First(x => x.Name.Contains("btnK"));
                    if (submitButton != null)
                    {
                        submitButton.InvokeMember("click");
                    }
                    else
                    {
                    MessageBox.Show("failed");
                }
                
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 24, 2019 5:53 AM
    Moderator