【问题标题】:Waiting between button events on a website在网站上的按钮事件之间等待
【发布时间】:2020-04-17 11:50:10
【问题描述】:

我想在两次点击事件之间等待,因为第二个事件取决于第一个事件的结果。

我的代码说清楚:

    '**************************************************************************
    'Adding piece number to the webbrowser control
    '26/12/2019 Kris Bauwens
    '**************************************************************************
    Web.Document.GetElementById("hwbOrPiece").InnerText = txtSID.Text
    Web.Document.GetElementById("hwbOrPiece").RemoveFocus()
    SearchHWB()

Private Sub SearchHWB()
    '**************************************************************************
    'Search if the piece number exist, if so look for the history
    '27/12/2019 Kris Bauwens
    '**************************************************************************
    Dim HtmlElementcol As HtmlElementCollection = Web.Document.GetElementsByTagName("button")
    For Each elem As HtmlElement In HtmlElementcol
        If elem.InnerText = "search " Then
            elem.Focus()
            'Invoke event
            elem.InvokeMember("click")
            Thread.Sleep(3000) ''''''''''''''''''''''''-> not really working
            Do While blnFind = False
                SearchHistory()
            Loop
            Exit For
        End If
    Next
    blnFind = False
End Sub

Private Function SearchHistory() As Boolean
    '**************************************************************************
    'Look for the history
    '27/12/2019 Kris Bauwens
    '**************************************************************************
    Dim HtmlElementcol As HtmlElementCollection = Web.Document.GetElementsByTagName("button")
    For Each elem As HtmlElement In HtmlElementcol
        Debug.Print(elem.InnerText)
        If elem.InnerText = "SD History " Then
            elem.Focus()
            'Invoke event
            elem.InvokeMember("click")
            blnFind = True
            Exit For
        End If
    Next
    Return blnFind
End Function

我用“线程”尝试过,但速度很快。系统已经从第二个事件开始,而不是等待第一个事件。它甚至不等待内部文本执行。

我该如何解决这个问题?

【问题讨论】:

  • 这不是它的工作原理。在第一个事件上,您设置一个Boolean 标志以指示该事件已发生。在第二个事件中,您检查该标志。你知道,就像在现实生活中一样。
  • 我尝试了几件事......
  • 当您点击按钮元素时页面是否会完全刷新?如果是这样,您可以在 SearchHistory 的 documentcomplete 事件中抛出逻辑。不过,我认为您似乎正在尝试操纵其他人的页面?使用 Fiddler 之类的东西并发送/接收原始数据与通过 Web 控件操作 DOM 相比可能更容易。这样你会得到更加一致的结果。
  • 不是完全刷新。我也在 VBA 中对其进行了编程,然后我得到了 Javascript / Angular 错误消息。

标签: vb.net webbrowser-control


【解决方案1】:

我用旧的 Application.DoEvents() 替换了 Thread.Sleep(Visual Basic 直到 1998 年) 我不喜欢它,还有其他建议吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    相关资源
    最近更新 更多