-2

So I need to press a button in webbrowser control but it gives me a null reference exception

Browser.Document.GetElementById("homebg").InvokeMember("click");

This is my browser

This is the source code in the webbrowser control

And I think I get the error because the content is pushed by javascript

So can anyone help me?

  • Check Browser.Document.GetElementById("homebg") is null or not before InvokeMember("click"); – Golda Oct 13 '17 at 09:27
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Fildor Oct 13 '17 at 09:28

2 Answers2

0

Have you tested the null-conditional operator? This would fix the exception.

Browser?.Document?.GetElementById("homebg")?.InvokeMember("click");

If this works then you could try to remove a questionmark one at a time to see where you get your exception from.

I would suggest you start with:

Browser?.Document?.GetElementById("homebg").InvokeMember("click");

I suspect that you dont have a element with the name homebg.

Alex Telon
  • 1,107
  • 1
  • 14
  • 30
  • Yes it gives the exception when I use `Browser?.Document?.GetElementById("homebg").InvokeMember("click");` – Dr. Professor Owl Oct 13 '17 at 09:35
  • Then this means that `GetElementById("homebg")` returns null. It tries to find an element with the name homebg but it does not find one. It seems you spelled it right so make sure you are reading the right document or that the document has actually loaded before you do you call (duno if that can happen in you application, but I suspect it might be an issue) – Alex Telon Oct 13 '17 at 09:41
0

Check for WebBrowser.ObjectForScripting property - this allows your java-script to communicate with the host. So whenever, your content/html is ready, you should invoke some method on this object from javascript that will signal the host application (c# code) that your browser content is ready and then you can simulate the click event.

For example, in C# code

public void OnContentReady() 
{
    // TODO invoke button click
}

...

// code loading webbrowser
browsercontrol.ObjectForScripting = this;

And in within your browser js code

// code that creates the html content
...

// signal the host that content is ready
window.external.OnContentReady();
VinayC
  • 47,395
  • 5
  • 59
  • 72
  • and with the code that creates the html content, you mean the javascript code right? – Dr. Professor Owl Oct 13 '17 at 10:54
  • Can you give me a bigger example of how it should look like? – Dr. Professor Owl Oct 13 '17 at 11:41
  • @Dr.ProfessorOwl, yes to your first question. For bigger example, you can see the example code in the ObjectForScripting property or you may use http://notions.okuda.ca/2009/06/11/calling-javascript-in-a-webbrowser-control-from-c/. Typically InvokeScript is a way to invoke js from c# and ObjectForScripting is way to invoke c# from js. All I am saying is that you need to wait till your html is ready and you can know that only within browser in java-script. So js code has to inform c# code that relevant html is loaded and c# code can now invoke it. – VinayC Oct 13 '17 at 12:27
  • Thanks a lot! I am not a very experienced programmer, so I don't exactly know how to do this, but I think I can figure it out. – Dr. Professor Owl Oct 13 '17 at 14:53