Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more: , +
I am learning how to automate tasks on Websites.I have an html snippet which contains a JavaScript Code which i wish to invoke programmatically using WebBrowser Control.I am using IE11 for Emulation.I cannot edit the Website Source.
How to invoke the JS Code.

HTML
<a style="removed: auto ! important;" id="13185-SL-CK-0" href="javascript:jpBook($('#13185-SL-CK-0'),'13185','SDAH','DBG','24-10-2015','SL','CK',3,false);" tabindex="1">Book Now</a>


In my MozillaBrowser Console when i paste the content of href i get the desired results(the JS is executed).But in WinForms. I have tried

C#
webBrowser1.Navigate("javascript:jpBook($('#13185-SL-CK-0'),'13185','SDAH','DBG','24-10-2015','SL','CK',3,false);");


C#
webBrowser1.Document.InvokeScript("javascript:jpBook($('#13185-SL-CK-1'),'13185','SDAH','DBG','24-10-2015','SL','CK',3,false);");



without any luck. A simple solution could be to access the button and click it but i don't want to do that
Posted
Updated 23-Oct-15 5:19am
v5

1 solution

Have you tried to follow this example on MSDN? WebBrowser.InvokeScript Method (String, Object[])[^]

Some times it helps to go back to basics and then add on more complexity.

You have to modify the JavaScript method, though.
HTML
<html>
    <head>
        <script type="text/javascript">
            // Function With Parameters
            function JavaScriptFunctionWithParameters(message)  
            {
              outputID.innerHTML = "JavaScript function 'called: " + message + ".";
            }
        </script>
    </head>
    <body>
    
        Hello from HTML document with script!
    
    </body>
</html>


Make sure that your document finished loading before you invoke the Java script.

You could try to add this meta data inside the <head> tag.
HTML
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

I think the WebBrowserControl defaults to IE7 otherwise.

If I remember correctly you also need to add your application to the registry in order to enable IE9.
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION]
"WDExpress.exe"=dword:0000270f
"devenv.exe"=dword:0000270f
"AcroRd32.exe"=dword:0000270f
"<Yourapplication>.exe"=dword:0000270f


Can't remember exactly where I found the information about this, but I think it was Stack Overflow.
Here is a similar link Regarding IE9 WebBrowser control[^]
 
Share this answer
 
v4
Comments
Rahul Anand Jha 23-Oct-15 11:21am    
My document is finished loading since it is only after sometime that i do click it.I have tried webBrowser1...invokeScript but it doesn't work.The script upon working is expected to take me to a new Page
George Jonsson 23-Oct-15 11:29am    
But have you tried with a more simple example?
Rahul Anand Jha 23-Oct-15 11:49am    
I do not have the permission to edit the Source.I have set the DWORD for Browser Emulation of App to 11001(IE 11)
George Jonsson 23-Oct-15 12:12pm    
But you can create your own HTML file and try to add the meta data just to see if it helps.
If it works you can look into how to inject HTML code into the <head> tag.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900