c# Webbrowser How to call the onclick function?

Asked

Viewed 27 times

0

How to call the function onclick of this html:

<a  
  href="javascript:;" 
  onclick="ExecutarLance('46368');"
  title="ExecutarLance" 
  class="btn btn-custom3">
    LANCE
</a> == $0
  • could detail better?

  • I would like to invoke the onclick function of this html, it would be with invokescript?

  • you did not because the entire code in your question could improve your question as you did?

  • You have the full website address?

  • You have the code you used to load the information.

  • I would like to do an automatic click on a button on a website. But the only attribute I can use is onclick, because the site has several buttons that use the same href, class and title, but if I use any of these the click will be on more than button at the same time

  • https://answall.com/questions/491468/como-usar-o-attributeonclick-em-c-webbrowser is a duplicate, you only need to open one.

Show 2 more comments

2 answers

1

You can directly invoke the script as follows:

webBrowser1.Document.InvokeScript("ExecutarLance", new[] { "46368" });

I think this might give you a way to solve your problem.

  • It worked, thank you.

0

You can use elemento.InvokeMember("Click"), where elemento is the Htmlelement that you want to call the onclick function.

HtmlElementCollection collection = webBrowser1.Document.GetElementsByTagName("a");
foreach(HtmlElement element in collection)
{
    if (element.GetAttribute("title") == "ExecutarLance")
    {
        element.InvokeMember("Click");
        break;
    }   
}

Note that in the code above I identified the element by the attribute title, but you can change to identify it in another more specific way.

EDIT: The correct function is InvokeMember

  • But on the site I want to click, there are several buttons that use the same title, href and class, if I use any of these the click will be in more than one location. The only different attribute is the onclick that the only one of the button I want to click.

  • then change the code to identify the element by the attribute onclick.

  • I tried this, the code would look: element.Getattribute("onclick") == "Executarlance('46368');"

  • But it doesn’t work

  • Really, it seems to read the attribute onclick of an element in C# does not work. But that would be another problem. See: https://www.codeproject.com/Questions/1074179/Get-javascript-onClick-function-definition-with-we

Browser other questions tagged

You are not signed in. Login or sign up in order to post.