本文介绍了使用VBA的下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用VBA从下拉菜单中选择特定选项。我如何才能做到这一点?https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist=链接到我们尝试从其中拉出的网页
我尝试的IE.document.getElementsByName("down_count").click代码
Full Module:
Private Sub Workbook_Open()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.application")
With IE
.Visible = True
.Navigate ("https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist=")
While .Busy Or .readyState <> 4: DoEvents: Wend
End With
With IE.document
IE.Refresh
Set div = IE.document.getElementById("save-list-link")
div.FireEvent "onclick"
' Application.SendKeys "{TAB}", True
' Application.SendKeys "{TAB}", True
' Application.SendKeys "{SPACE}", True
' Application.SendKeys "{DOWN}", True
' Application.SendKeys "{ENTER}", True
' Application.SendKeys "{TAB}", True
' Application.SendKeys "{TAB}", True
' Application.SendKeys "{SPACE}", True
' Application.SendKeys "{DOWN}", True
' Application.SendKeys "{DOWN}", True
' Application.SendKeys "{DOWN}", True
' Application.SendKeys "{ENTER}", True
IE.document.getElementsByName("down_count").click
' For Each elt In IE.document.getElementById("number-of-studies")
' If InStr(elt.innerText, "Found") > 0 Then elt.click: Exit For
' Next elt
Set div4 = IE.document.getElementById("submit-download-list")
div4.click
End With
End Sub
希望将研究数量转到所有找到的(此编号更改)并将选择的文件格式设置为CSV推荐答案
下面介绍如何下载
Option Explicit
'VBE > Tools > References: Microsoft Internet Controls
Public Sub GetData()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate2 "https://clinicaltrials.gov/ct2/results?cond=&term=Medpace&cntry=&state=&city=&dist="
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
.querySelector("#save-list-link").Click
.querySelector("#number-of-studies option:last-child").Selected = True 'select last option from that dropdown
'.querySelector("#number-of-studies").selectedIndex = 1 ''selects 2nd option
.querySelector("[value=csv]").Selected = True 'select csv with attribute = value css selector
.querySelector("#submit-download-list").Click
End With
Application.Wait Now + TimeSerial(0, 0, 10)
Application.SendKeys "%+O", True
Application.Wait Now + TimeSerial(0, 0, 10)
.Quit
End With
End Sub
这篇关于使用VBA的下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!