Can you please tell me how to click buttons on the site programmatically through the webbrowser component?

  • Again? What does Win API have to do with it? - Igor
  • Sori, I meant winforms - Pyrejkee
  • You, it seems, have already found webBrowser1.Document.GetElementById("buttonId").InvokeMember("click"); - Igor
  • In "buttonId" I just need to substitute the name of the desired button? Suppose there is a button on the Buy site, then it will be so webBrowser1.Document.GetElementById("Купить").InvokeMember("click"); - Pyrejkee
  • Using the browser's “View Source” or F12, find this button and look at the value of the id attribute. <button type="submit" id="btnBuy">Купить</button> - Igor

1 answer 1

 var elements = webBrowser1.Document.GetElementsByTagName("input"); foreach (HtmlElement element in elements) { string attrValue = element.getAttribute("value"); if (attrValue == "Купить") { element.InvokeMember("click"); } } 
  • It does not work; (I made a button that clicking on it would press a button on the site, press a button, nothing happens - Pyrejkee
  • Guys, help, still did not understand. - Pyrejkee
  • @KirillKiryanchikov It's time for you to upload your code. "made a button ... press a button" - can mean anything. Specify at least a link to the page you are trying to manage. - Igor
  • On the form of the component is a web browser and my button, I want to click on my button - click the button on the site. I can’t do it private void button1_Click(object sender, EventArgs e) { var elements = webBrowser1.Document.GetElementsByTagName("input"); foreach (HtmlElement element in elements) { if (element.GetAttribute("value") == "Купить") { element.InvokeMember("click"); } } } - Pyrejkee
  • Put the breakpoint on the line with var elements = ... , and go attrValue (F10) looking at the value of attrValue . - Igor