VBA更改Internet Explorer中的下拉菜单值 [英] VBA to change dropdown value in internet explorer

查看:307
本文介绍了VBA更改Internet Explorer中的下拉菜单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用Excel VBA自动化Internet Explorer,以从网站中提取足球结果,并且当我更改下拉列表值时,我真的在努力使数据进行更新.

I am looking to automate internet explorer using Excel VBA to extract football results from a website and am really struggling with getting the data to update when I change the dropdown value.

该网站为:我希望更改阶段"下拉列表的值并抓取匹配结果.

I am looking to change the value of the 'stages' dropdown and scrape the match results.

我的代码可以很好地用于打开IE,更改抓取"下拉列表的值,但我无法更新数据.虽然我对VBA感到满意,但是我对HTML和Javascript的了解很少,尽管我可以猜测某些行在做什么.从我看到的内容中可以看到处理更改事件的javascript代码,我只是看不到如何运行它-我已尝试根据搜索提示在代码中触发"onchange"事件,但我无法使它正常工作.

My code works fine for opening IE, changing the value of the 'scrape' dropdown but I can't get the data to update. Whilst I am comfortable with VBA I know very little about HTML and Javascript although I can guess what some lines are doing. From what I can see there is javascript code that handles the change event, I just can't see how to get it to run - I have tried firing the 'onchange' event in my code as suggested from my searches but I can't get it to work.

这是我可以看到的控制下拉菜单的代码(我删除了许多其他下拉菜单的下拉菜单值,因为它使这篇文章更长:

This is the code I can see that controls the drop down (I have deleted a lot of the dropdown values for other dropdowns as it made this post even longer:

<div id="breadcrumb-nav">
.
.
<span><select id="stages" name="stages"><option selected="selected"     value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8209">Europa League Group Stages</option>
<option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/7816">Europa League  Qualification</option>
<option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8158">Europa League Grp. A</option>
<option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8159">Europa League Grp. B</option>
.
.
<option value="/Regions/250/Tournaments/30/Seasons/3871/Stages/8466">Europa League</option>
</select></span>


</div>


<script type="text/javascript">

$('#breadcrumb-nav select').change(function () {
NG.GA.trackEvent('BreadcrumbNav', this.id);
window.location.href = this.value;
// TODO: Disable all selects?
});

</script>

我的代码:

Sub ScrapeData()
Dim ie As InternetExplorer
Dim URL As String

URL = "http://www.whoscored.com/Regions/250/Tournaments/30/Seasons/3871/Stages/8466/Fixtures/Europe-UEFA-Europa-League-2013-2014"

Set ie = New InternetExplorer
ie.Visible = True
ie.navigate (URL)

Do
    DoEvents
Loop Until ie.readyState = 4

SelectValue ie, "/Regions/250/Tournaments/30/Seasons/3871/Stages/7816"
SelectValue ie, "/Regions/250/Tournaments/30/Seasons/3871/Stages/8209"

End Sub

Sub SelectValue(ByVal ie As InternetExplorer, ByVal value As String)
Dim htmlDoc As HTMLDocument
Dim ddStages As HTMLSelectElement
Dim idBreadCrumb As Object

Set htmlDoc = ie.document

With ie.document
    Set idBreadCrumb = .getelementbyid("breadcrumb-nav")
    Set ddStages = .getelementbyid("stages")
End With

ddStages.value = value
ddStages.FireEvent ("onchange")
'fireevent on ddStages didn't work so tried here too
idBreadCrumb.FireEvent ("onchange")

Do
    DoEvents
Loop Until ie.readyState = 4

End Sub

任何帮助将不胜感激.

推荐答案

在选择元素已更改其值"事件上必须执行一些JavaScript.我的建议比执行JavaScript容易得多,只是浏览链接(因为JS在这里所做的只是更改您所看到的HTML页面,而不是更改同一网页中的元素).

There must be some JavaScript executing on the event "the select element has changed its value". My suggestion, much easier than executing the JavaScript, is to just navigate the link (because what the JS does here is just changing the HTML page you are seeing, and not the elements within the same webpage).

例如,我将替换为:

SelectValue ie, "/Regions/250/Tournaments/30/Seasons/3871/Stages/7816"

与此

ie.Navigate "http://www.whoscored.com/" & "/Regions/250/Tournaments/30/Seasons/3871/Stages/7816"

以获得完全相同的结果.

to get the exactly same result.

这篇关于VBA更改Internet Explorer中的下拉菜单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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