【问题标题】:Changing HTML elements with Webbrowser使用 Webbrowser 更改 HTML 元素
【发布时间】:2017-11-03 19:33:40
【问题描述】:

我有一个问题,如何更改 HTML 输入值?

<select name="sys_lenght" aria-controls="systable" class>
<option value="20">Value 1</option>
<option value="40">Value 2</option>
<option value="80">Value 3</option>
</select>

我用过这段代码

For Each Elementz In WebBrowser1.Document.GetElementsByTagName("select")
        If Elementz.Name = "sys_lenght" Then
            Elementz.setAttribute("value", "80")
        End If
    Next

但它不会更改输入值,只会更改文本“Value 3”。 我怎么解决这个问题?谢谢

【问题讨论】:

    标签: vb.net webbrowser-control


    【解决方案1】:

    sys_lenght 的值表示选择了哪个具有指定值的选项。因此,如果您将sys_lenght.value 设置为"80",它将选择Value 3

    要更改当前选择的选项value,您必须首先获得对该选项的引用。您可以通过获取sys_lenghtselectedIndex 来做到这一点,然后从该索引中获取特定项目。

    For Each Elementz In WebBrowser1.Document.GetElementsByTagName("select")
        If Elementz.Name = "sys_lenght" Then
    
            'Get the index of the selected option.
            Dim SelectedIndex As Integer = Integer.Parse(Elementz.GetAttribute("selectedIndex"))
            If SelectedIndex >= 0 Then
                'Get the option element from the resulting index.
                Dim SelectedOption As HtmlElement = Elementz.Children(SelectedIndex)
                SelectedOption.setAttribute("value", "80")
            Else 'No option selected.
                MessageBox.Show("No option selected!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
    
            Exit For 'We found what we were looking for; stop looping.
        End If
    Next
    

    【讨论】:

    • 可以创建新的自定义值吗?例如:300?
    • @Giuseppe :是的,您可以将其设置为您想要的任何内容(网站是否会接受这是一个不同的问题)。
    • 好吧,仍然不适合我,没有错误,但仍然没有改变:\
    • @Giuseppe :您到底想改变什么?当我说您要更改所选option 的值时,我说得对吗?
    • @Giuseppe :您是否真的确定它实际上并没有改变,而且情况并不是因为网页拒绝价值? (不管它用它做什么) - 因为我刚刚测试了代码,它对我来说很好。
    【解决方案2】:

    首先你必须了解html如何打开和关闭标签,你忘了关闭html选择选项打开标签我的意思是最后一个greter符号>然后使用javascript查询在选项示例中查找值

    WebControl1.ExecuteJavascript('document.querySelector('option [value='your value']').selected=true;") 
    

    希望得到帮助?

    【讨论】:

    • 我的错误,已修复。但这对这个问题没有任何影响
    • 我看到你已经更新了你的答案,嗯......仍然无法使用这个 javascript 注入
    猜你喜欢
    • 1970-01-01
    • 2016-06-17
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多