vb.net Web自动化-触发事件 [英] vb.net web automation - triggering events

查看:93
本文介绍了vb.net Web自动化-触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以在遇到小问题时给予我帮助.可能并不困难,但这是我第一次尝试进行Web自动化,但我陷入了困境.

我正在将WebBrowser控件与VB.Net(2010 Express)结合使用,以从ft.com获取汇率. (注意:我意识到这样做的方法比较简单,但这是一项学习练习,与其他方法一样多).我导航到一个网页,设置一个文本框和两个列表框的值,然后尝试获取将显示为文本的汇率.问题是,没有显示结果.我可能需要触发一个事件来强制页面更新,但是我已经尝试过了,但是它没有用.

我正在做的是:
1.导航到"http://markets.ft.com/research/Markets/Currencies"
2.将金额设置为1(HTML元素数量")
3.将从"货币设置为GBP(列表索引1; HTML元素"CurrencyFrom")
4.将"to"货币设置为EUR(列表索引12; HTML元素"CurrencyTo")

这里没有给出步骤1的代码,因为它是一个单独的功能,并且很容易解释.页面完全加载后,将运行以下事件过程:

I hope somebody might be able to give me assistance with a small problem. Probably not difficult, but this is my first attempt at web automation and I have got stuck.

I am using the WebBrowser control with VB.Net (2010 Express) to get exchange rates from ft.com. (Note: I realise that there are simpler ways of doing this, but it is a learning exercise as much as anything else). I am navigating to a webpage, setting the values of a textbox and two listboxes, and then trying to get the resulting exchange rate which will be displayed as text. Trouble is, the result is not being displayed. I probably need to trigger an event to force the page to update, but I have tried this and it hasn''t worked.

What I am doing is:
1. Navigate to "http://markets.ft.com/research/Markets/Currencies"
2. Set amount to 1 (HTML element "quantity")
3. Set ''from'' currency to GBP (list index 1 ; HTML element "CurrencyFrom")
4. Set ''to'' currency to EUR (list index 12 ; HTML element "CurrencyTo")

Code for step 1 is not given here since it is in a separate function and is fairly self explanatory. Once the page is fully loaded, the following event procedure runs:

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, _
        ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

    With WebBrowser1
        If .ReadyState = WebBrowserReadyState.Complete Then
            .Document.All.Item("quantity").InnerText = 1
            .Document.All.Item("CurrencyFrom").SetAttribute("SelectedIndex", 1)
            .Document.All.Item("CurrencyTo").SetAttribute("SelectedIndex", 12)
            .Document.All.Item("CurrencyFrom").RaiseEvent("onChange")
        end if
    end with



"onchange"事件旨在触发更新,但并非如此.我尝试了几种变体而没有成功,包括文本框中的onKeyDown事件.因此,目前已在页面上正确设置了金额和to/from货币,但未显示结果汇率.

关于如何最好地做到这一点的任何建议将不胜感激.



The ''onchange'' event is intended to trigger an update, but it doesn''t. I have tried a few variations without success, including an onKeyDown event in the textbox. So at the moment the amount and the to/from currencies are correctly set on the page, but the resulting rate is not shown.

Any suggestions on how best to do this would be appreciated.

推荐答案

使用 ^ ].
Use InvokeMember[^] instead.
.Document.All.Item("CurrencyFrom").InvokeMember("onchange")


这篇关于vb.net Web自动化-触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆