You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET WebBrowser转WebView2:日期输入框赋值问题求助

问题:WebView2无法通过data-ig属性为日期input元素赋值

之前在VB.NET中使用旧版WebBrowser控件编写的代码可正常为网站上的input元素设置日期值——通过遍历input元素,匹配其data-ig属性找到对应控件,调用focus并设置value。但迁移至WebView2控件后,尝试多段JavaScript代码均无法为目标input元素(带有指定data-ig属性)赋值,仅通过id选择元素的代码可正常工作,恳请提供可行解决方法。

注:records_array(z)(4)等为数组中的日期,格式为"06/01/2024"

原WebBrowser工作代码

Dim inputs As HtmlElementCollection = webBrowser1.Document.GetElementsByTagName("input")

For Each input As HtmlElement In inputs

    If Not IsNothing(input) Then

        If input.GetAttribute("data-ig") = "x:939876968.0:mkr:3" Then

            input.InvokeMember("focus")
            input.SetAttribute("value", format_CHD_date(records_array(z)(6)))
            add_to_log(log_window, "Passing Admit Date value to site = " & format_CHD_date(records_array(z)(6)))
            Application.DoEvents()

        End If

        If input.GetAttribute("data-ig") = "x:136274528.0:mkr:3" Then

            input.InvokeMember("focus")
            input.SetAttribute("value", format_CHD_date(records_array(z)(7)))
            add_to_log(log_window, "Passing Discharge Date value to site = " & format_CHD_date(records_array(z)(7)))
            Application.DoEvents()

        End If

        If input.GetAttribute("data-ig") = "x:101172535.0:mkr:3" Then

            input.InvokeMember("focus")
            input.SetAttribute("value", format_CHD_date(records_array(z)(4)))
            add_to_log(log_window, "Passing DOB value to site = " & format_CHD_date(records_array(z)(4)))
            Application.DoEvents()

        End If

    End If

Next

页面及元素截图

网站视图
代码截图

WebView2中可正常工作的代码(通过id选择元素)

Await WebView21.CoreWebView2.ExecuteScriptAsync("document.getElementById('ctl00_MainBody_ctl00_lastTextBox').invoke;")
Await WebView21.CoreWebView2.ExecuteScriptAsync("document.getElementById('ctl00_MainBody_ctl00_lastTextBox').setAttribute('value', '" & records_array(int_next)(2) & "');")

尝试过的无效代码(无法为目标元素赋值)

Await WebView21.CoreWebView2.ExecuteScriptAsync("document.querySelector('[data-ig='x:939876968.0:mkr:3']').focus();")
Await WebView21.CoreWebView2.ExecuteScriptAsync("document.querySelector('[data-ig='x:939876968.0:mkr:3']').value = '01/15/2024';")

Await WebView21.CoreWebView2.ExecuteScriptAsync("document.querySelector('[data-ig='x:939876968.0:mkr:3']').focus();")
Await WebView21.CoreWebView2.ExecuteScriptAsync("document.querySelector('[data-ig='x:939876968.0:mkr:3']').setAttribute('value', '01/15/2024');")

Await WebView21.CoreWebView2.ExecuteScriptAsync("document.querySelector('[data-ig='x:939876968.0:mkr:3']').invoke();")
Await WebView21.CoreWebView2.ExecuteScriptAsync("document.querySelector('[data-ig='x:939876968.0:mkr:3']').value = '01/15/2024';")

Await WebView21.CoreWebView2.ExecuteScriptAsync("document.querySelector('[data-ig='x:939876968.0:mkr:3']').invoke();")
Await WebView21.CoreWebView2.ExecuteScriptAsync("document.querySelector('[data-ig='x:939876968.0:mkr:3']').setAttribute('value', '01/15/2024');")

内容的提问来源于stack exchange,提问作者Always Try to Learn More

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 05:26:18