Click here to Skip to main content
15,035,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, to give a gist of what i am trying to do is, I am launching a webpage,
once the page is launched and loaded, the page has a drop down,
I am trying to select a value, the value get selected but there is an onchange
event associated with it, which doesn't get fired. can anyone help me in how do i
fire that event, as upon firing of the event, there are other controls which get
enables. At present once the value in the drop down is selected, the other controls
do not get enabled.

C#
InternetExplorer WebSiteURL = new InternetExplorer(); // created object for IR 
                WebSiteURL.Navigate("example.com"); // navigating to sign in page url
                System.Threading.Thread.Sleep(5000);
                HTMLDocument myDoc = new HTMLDocument();//creating document object to store page text
                WebSiteURL.Visible = true;
                System.Threading.Thread.Sleep(5000);
                myDoc = (HTMLDocument)WebSiteURL.Document;
                System.Threading.Thread.Sleep(5000);
                SHDocVw.WebBrowser webBrowser1 = new SHDocVw.WebBrowser();
                HtmlElement theList = webBrowser1.Document.GetElementById("ST_SEARCH_TYPE");
                if (myDoc != null)
                {
                    var dropdown = ((IHTMLElement)myDoc.all.item("ST_SEARCH_TYPE"));
                    var dropdownItems = (IHTMLElementCollection)dropdown.children;
                    foreach (IHTMLElement option in dropdownItems)
                    {
                        var value = option.getAttribute("value").ToString();
                        if (value.Equals("P"))
                            option.setAttribute("selected", "selected");
                    }
                }

This the html code.


-- Select --
PAN - New / Change Request
TAN - New / Change Request

What I have tried:

Have tried with invokescript but no luck.
Posted
Updated 20-Feb-17 19:49pm
v2

Do this from webBrowser1_DocumentComplete(...) event:
C#
var htmlDocument = webBrowser1.Document as IHTMLDocument2;
  if (myDoc!= null)
  {
     var dropdown = ((IHTMLElement)myDoc.all.item("ST_SEARCH_TYPE"));
     var dropdownItems = (IHTMLElementCollection)dropdown.children;

     foreach(IHTMLElement option in dropdownItems)
     {
        var value = option.getAttribute("value").ToString();
        if(value.Equals("P"))
           option.setAttribute("selected", "selected");
     }

  }
   
Comments
digengada 21-Feb-17 1:45am
   
I have not used webbrowser at all. as its not working in winforms. any suggestion on how to use it?
Graeme_Grant 21-Feb-17 1:48am
   
What is this then?
HDocVw.WebBrowser webBrowser1 = new SHDocVw.WebBrowser();

Edit: Why your code fails is due to the page loading asynchronously, so your code executes (synchronously) before the page is ready. That is why you need to hook the DocumentComplete event and then process.
digengada 21-Feb-17 1:55am
   
Can you please help me with the code?
   

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