javascript 以编程方式从 .NET Webbrowser Control 的下拉列表中选择一个项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15265539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically select an item from a drop down in .NET Webbrowser Control
提问by Muhammad Sharjeel Ahsan
Below is a html script, I grabbed from a website. I wanna select the item programmatically using .NET
下面是一个 html 脚本,我从一个网站上抓取。我想使用 .NET 以编程方式选择项目
<div id="MySite.condition_s-wrp" class="c-wrp-slctbx" style="z-index: 1;">
<input id="MySite.condition_s-input" type="text" autocomplete="off" readonly="readonly" tabindex="0" class=" c-slctbx-medium" style="width: 268px;">
<ul class="c-ul-slctbx max_height_300" style="width: 285px; display: none; top: 21px;">
<li id="MySite.condition_s-option-" class="c-li-slctbx">Please choose</li>
<li id="MySite.condition_s-option-First" class="c-li-slctbx">First</li>
<li id="MySite.condition_s-option-Second" class="c-li-slctbx">Second</li>
</ul>
<select id="MySite.condition_s" name="attributeMap[MySite.condition_s]" class=" c-slctbx-medium" style="display: none;">
<option value="">Please choose</option>
<option value="First">First</option>
<option value="Second">Second</option>
</select>
</div>
Please note the following code is not working at all.
请注意以下代码根本不起作用。
webBrowser1.Document.GetElementById("MySite.condition_s").SetAttribute("value", "First");
Any quick help will be highly appreciated.
任何快速帮助将不胜感激。
采纳答案by Muhammad Sharjeel Ahsan
Finally I figure it out with one of my friends. This little function will do the rest very easily.
最后我和我的一个朋友弄清楚了。这个小功能会很容易地完成其余的工作。
Thanks to Farrukh Momin and his time.
感谢 Farrukh Momin 和他的时间。
public void SetComboItem(string id, string value) {
HtmlElement ee = this.webBrowser1.Document.GetElementById(id);
foreach (HtmlElement item in ee.Children) {
if (item.OuterHtml.ToLower().IndexOf(value.ToLower()) >= 0) {
item.SetAttribute("selected", "selected");
item.InvokeMember("onChange");
}
else {
item.SetAttribute("selected", "");
}
}
ee = this.webBrowser1.Document.GetElementById(id + "-input");
ee.InnerText = value;
}
Calling Function
调用函数
this.SetComboItem("MySite.condition_s", "First");
回答by Bart Friederichs
Have you tried this:
你有没有试过这个:
webBrowser1.Document.GetElementById("MySite.condition_s").selectedIndex = 1
回答by scartag
Try this.
试试这个。
HtmlDocument document = webBrowser1.Document;
HtmlElement siteCondition = document.GetElementById("MySite.condition_s");
var option = siteCondition.Children.Cast<HtmlElement>().First(x => x.GetAttribute("value").Equals("First"));
option.SetAttribute("selected", "selected");
回答by Amrendra
here is your solution just go through example: http://www.vbforums.com/showthread.php?701093-Webbrowser-Control-Select-Dropdownlists-option
这是您的解决方案,只需通过示例:http: //www.vbforums.com/showthread.php?701093-Webbrowser-Control-Select-Dropdownlists-option
or
或者
回答by Photonic
I founded that if you just invoke click one by one, you should be able to find what you want by doing a for loop click inside it.
我发现如果你只是一个一个地调用 click ,你应该能够通过在里面做一个 for 循环 click 来找到你想要的东西。
HtmlElement site = this.webBrowser2.Document.GetElementById("myId");
foreach (HtmlElement item in site.Children)
{
if (item.InnerText.ToString() == "something")
{
item.InvokeMember("Click");
break;
}
else
{
item.InvokeMember("Click");
}
}
回答by mr.baby123
100% working code (tested on win7 - ie11)
100% 工作代码(在 win7 - ie11 上测试)
taken from:
取自:
c# | WebBrowser control - programmatically select item on html select
http://mdb-blog.blogspot.com/2016/12/c-browser-control-programmatically.html
C# | WebBrowser 控件 - 以编程方式选择 html 上的项目选择
http://mdb-blog.blogspot.com/2016/12/c-browser-control-programmatically.html
HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("select")
foreach (HtmlElement heItem in col)
{
if (heItem.GetAttribute("className").Contains("exampleClassName") == true)
{
heItem.SetAttribute("selectedIndex", "3"); // select value at #3
break; // incase of needed...
}
}