gpt4 book ai didi

c# - 从网络浏览器中选择下拉选项

转载 作者:太空狗 更新时间:2023-10-29 22:33:24 25 4
gpt4 key购买 nike

我的目标我需要选择第二个选项。

我尝试了以下方法,但无法设置选定值。没有错误出现,选择只是没有发生。我自己非常熟悉 HTML,我知道“selected”和“selected="selected"”可以工作,但不确定为什么它不能与我的 C# 代码一起工作。有什么问题吗?

webBrowser1.Document.GetElementById("field_gender1").
Children[1].SetAttribute("selected", "selected");

HTML 是

<select name="gender1" id="field_gender1" class="select">
<option selected="selected" value="1">val1</option>
<option value="2">val2</option>
</select>

最佳答案

从 WebBrowser_DocumentComplete(...) 执行此操作

var htmlDocument = webBrowser1.Document as IHTMLDocument2;
if (htmlDocument != null)
{
var dropdown = ((IHTMLElement)htmlDocument.all.item("field_gender1"));
var dropdownItems = (IHTMLElementCollection)dropdown.children;

foreach(IHTMLElement option in dropdownItems)
{
var value = option.getAttribute("value").ToString();
if(value.Equals("2"))
option.setAttribute("selected", "selected");
}

}

关于c# - 从网络浏览器中选择下拉选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12257756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com