Click here to Skip to main content
15,843,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Here is the rest of the code if it may help:
C#
          HtmlElementCollection theElementCollection;
          HtmlDocument filters = webBrowser1.Document;

          theElementCollection = webBrowser1.Document.GetElementsByTagName("input");

          HtmlElementCollection pCollection = filters.GetElementById("filterSortBy").GetElementsByTagName("option");
          List<HtmlElement> pList = new List<HtmlElement>();
          foreach (HtmlElement pItem in pCollection) { pList.Add(pItem); }
          HtmlElement pElement =
            (HtmlElement)pList.Where(pOption => pOption.GetAttribute("value").Equals("price_low_high", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();

          if (pElement.GetAttribute("value").Equals("price_low_high"))
          {
              pElement.SetAttribute("Selected", "price_low_high");
              pElement.InvokeMember("click");
          }

          pList.Clear();
          pCollection = filters.GetElementById("filterAvailability").GetElementsByTagName("option");
          foreach (HtmlElement avItem in pCollection)
          {
              pList.Add(avItem);
          }

pElement = pList.Where(pOption => pOption.GetAttribute("value").Equals("1")).SingleOrDefault();  // After this statement the pElement is set to null.

          if (pElement.GetAttribute("value").Equals("1"))
          {
              pElement.SetAttribute("Selected", "1");
              pElement.InvokeMember("click");
          }

Error point noted in comment on pElement = pList.Where... line.

Is it because I want the value to be equal to 1?

What I am trying to do is select the value option named 1 (in this website) from a drop down list

The variables are not declared in this statement because it was used in other areas of the code. I tried declaring new variables but still the same result

This is the website element inspection:

XML
<select name="availability" id="filterAvailability">
    <option value="" label="All" selected="selected">All</option>
    <option value="1" label="In stock (793)">In stock (793)</option>
    <option value="2" label="Pre-order (7)">Pre-order (7)</option>
</select>


I was thinking more about this problem and I think the problem is the Element list I made as it does not capture values that are numbers, only text. that is my assumption. Is there any list that accepts numbers that can be used for the webbrowser?

Thanks.
Posted
Updated 28-Oct-13 13:53pm
v8
Comments
Ziee-M 29-Oct-13 3:38am    
after invoking pElement.InvokeMember("click";), the web page in the webbrowser need some time to refresh! webbrowser is an async control, you have to wait until the change is completed
slayasty 29-Oct-13 7:18am    
i see. ill try that and come back with the results
slayasty 29-Oct-13 8:36am    
So i tried using systems.timer to make a delay. The following code:

System.Timers.Timer myTimer = new System.Timers.Timer(5000);
myTimer.SynchronizingObject = webBrowser1;
myTimer.Enabled = true;
myTimer.Start();
myTimer.Stop();

the problem is the timer is not engaging. what am I doing wrong? It is completely bypassing the timer (as the 5 seconds are not timed)

thanks
Ziee-M 29-Oct-13 9:22am    
you can use this code to wait for the webbrowser to complete loading the page
while(webbrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
this is a temporary solution, it will break if you have a long running loop.
you will have to use threads later and ensure synchronisation.

Ps: after the web page is charged, you have to get your tags again since the html code will be changed!
slayasty 29-Oct-13 10:10am    
mm I changed abit that code. used an if condition if the readystate == webbrowserreadystate.complete. inside the if condition I transfered that filter availability code with the avList but it still didnt work :/

if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
//Application.DoEvents();
bookCollection = webBrowser1.Document.GetElementById("filterAvailability").GetElementsByTagName("option");

List<HtmlElement> avList = new List<HtmlElement>();
foreach (HtmlElement avItem in bookCollection)
{
avList.Add(avItem);
}

pElement = avList.Where(avOption => avOption.GetAttribute("value").Equals("1")).FirstOrDefault();

if (pElement.GetAttribute("value").Equals("1"))
{
pElement.SetAttribute("Selected", "1");
pElement.InvokeMember("click");
}
}

im not sure what difference does application.doevents() makes if it is the code or not tho.

i made the if condition because it makes sense that way because the filter of availability option can be chosen once the search button has been clicked

The .SingleOrDefault() will return null if there are no elements of pList that pass the .Where() condition.
So, are you sure the pList actually contains the option HtmlElement with the attribute "value" == 1??
 
Share this answer
 
Comments
slayasty 28-Oct-13 14:50pm    
well the pList shows only 1 count and its the All option value i dont know why. as it should have 3 values I think. considering the code I gave
Matt T Heffron 28-Oct-13 15:19pm    
So now you're one step closer to finding the real problem.
slayasty 28-Oct-13 15:23pm    
thats true but I cant figure out why the pList only shows one count. cant be because the option values are numbers can it?
Matt T Heffron 28-Oct-13 15:29pm    
You're now out of the area where I have any experience (ASP coding) so I can't offer any further assistance.
I suggest using the "Improve question" above and adding more to the description of the problem (including code) so someone with more relevant experience can advise.
Good luck.
slayasty 28-Oct-13 15:44pm    
i c. ok thanks :)
C#
if(webBrowser1.ReadyState == WebBrowserReadyState.Complete)
       {
           Application.DoEvents();
           bookCollection = webBrowser1.Document.GetElementById("filterAvailability").GetElementsByTagName("option");

           List<HtmlElement> avList = new List<HtmlElement>();
           foreach (HtmlElement avItem in bookCollection)
           {
               avList.Add(avItem);
           }

           pElement = avList.Where(avOption => avOption.GetAttribute("value").Equals("1")).FirstOrDefault();

           if (pElement.GetAttribute("value").Equals("1"))
           {
               pElement.SetAttribute("Selected", "1");
               pElement.InvokeMember("click");
           }


       }



thanks to both of you I solved it :) Thanks you for all the help
 
Share this answer
 
C#
nope! you have to use that block to wait for the application to load the page

if(webBrowser1.ReadyState == WebBrowserReadyState.Complete)
       {
           Application.DoEvents(); 
       }
//here the page is fully loaded
//continue your logic
          bookCollection = webBrowser1.Document.GetElementById("filterAvailability").GetElementsByTagName("option");
 
           List<HtmlElement> avList = new List<HtmlElement>();
           foreach (HtmlElement avItem in bookCollection)
           {
               avList.Add(avItem);
           }
 
           pElement = avList.Where(avOption => avOption.GetAttribute("value").Equals("1")).FirstOrDefault();
 
           if (pElement.GetAttribute("value").Equals("1"))
           {
               pElement.SetAttribute("Selected", "1");
               pElement.InvokeMember("click");
           }
 
Share this answer
 

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