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

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 :

HTML
<select name = "yr">
<option value= "">Year</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
</select>


error in my code:
C#
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));
    }
}




error:
A query body must end with a select clause or a group clause
Posted
Updated 15-Aug-13 6:30am
v2

1 solution

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

C#
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
 
v2

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