Click here to Skip to main content
15,846,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I'm working on a webpage automation project. And I am very new in this. I want to use Webbrowser control to select an item in the dropdownlist. Now,how do I programmetically select an item from the HTML snippet below. I would like to select,say for example,"2009" from this HTML code :

<select name = "yr">
<option value= "">Year</option>

error in my code:
dynamic TheItem = (from X in webBrowser1.Document.GetElementsByTagName("select").Cast<HtmlElement>()where X.GetAttribute("name") == "starting_date" X.GetElementsByTagName("option").FirstOrDefault;
if (TheItem != null)
    dynamic Item = (from X in TheItem.Cast <HtmlElement>() where X.InnerText == "18").FirstOrDefault;
    if (Item != null)
        Item.SetAttribute("Selected", Convert.ToString(true));

A query body must end with a select clause or a group clause
Updated 15-Aug-13 7:30am

1 solution

Your LINQ query isn't structured correctly. Try this...

var optionItems = (from X in webBrowser1.Document.GetElementsByTagName("select").Cast<HtmlElement>()
where X.GetAttribute("name") == "starting_date" select X.GetElementsByTagName("option"));

if (optionItems != null) 
    dynamic optionToSelect = (from X in optionItems.Cast<HtmlElement>() where X.InnerText == "18" select X).FirstOrDefault();

    if (optionToSelect != null) 
        optionToSelect.SetAttribute("Selected", Convert.ToString(true));
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