如何选择下拉值 - Vb.net Webbrowser

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16467776/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:33:20  来源:igfitidea点击:

How to select drop down value - Vb.net Webbrowser

vb.netbrowser

提问by user1908555

I want to select a dropdown value with vb.net webbrowser

我想用 vb.net webbrowser 选择一个下拉值

<OPTION value=1>One</OPTION> 
<OPTION value=2>Two</OPTION>

The values 1 & 2 are without the quotes

值 1 和 2 没有引号

If the value is within quotes such as value = "1"

如果值在引号内,例如 value = "1"

Then I can use the code

然后我可以使用代码

WebBrowser1.Document.GetElementById("ID").SetAttribute("Value", "1")

But it does not work for the above.

但它不适用于上述情况。

Thanks in advance.

提前致谢。

Actually it does not matter if there is a quote or not. I created test code and it seems to be working.

实际上,有没有引用并不重要。我创建了测试代码,它似乎工作正常。

For Each Frame As HtmlWindow In currentWindow.Frames
  Dim btnElementCollection As HtmlElementCollection = 
                 Frame.Document.GetElementsByTagName("Select")
    For Each curElement As HtmlElement In btnElementCollection
      Dim controlName As String = curElement.GetAttribute("id").ToString
      If controlName = TextBox2.Text Then
        curElement.SetAttribute("Value", TextBox3.Text)
      End If
    Next
Next

                                                                                     TextBox2 is the id TextBox3 is the value

Sorry, not sure why it didn't work the first time and thanks for everyones time.

抱歉,不知道为什么它第一次不起作用,谢谢大家的时间。

回答by Chris Kdon

Try setting the attribute name to uppercase.

尝试将属性名称设置为大写。

<OPTION VALUE=1>One</OPTION> 
<OPTION VALUE=2>Two</OPTION>

Note "value" is now "VALUE"

注意“值”现在是“值”

回答by Atif khan

You should set id of the target element. This should be

您应该设置目标元素的 id。这应该是

GetElementById("ID") 

this

这个

GetElementById("element id ")

View your page source to get the correct id.

查看您的页面源以获取正确的 ID。