选择webbrowser下拉列表项 [英] Select Item of webbrowser dropdown list
本文介绍了选择webbrowser下拉列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我正在开展一个网页自动化项目。我在这方面很新。我想使用Webbrowser控件来选择下拉列表中的项目。现在,如何从下面的HTML代码段中选择一个项目。我想从这个HTML代码中选择,例如2009:
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 :
<select name = "yr">
<option value= "">Year</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
</select>
$ b我的代码中有$ b错误:
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));
}
}
错误:
查询正文必须以select子句或group子句结束
error:
A query body must end with a select clause or a group clause
推荐答案
您的LINQ查询不是结构化的正确。试试这个...
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));
}
}
这篇关于选择webbrowser下拉列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文