Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何使用FireEvent“触发网页更新”;“一旦更改”;,在Internet Explorer中从下拉列表中选择时?_Html_Excel_Vba_Onchange - Fatal编程技术网

Html 如何使用FireEvent“触发网页更新”;“一旦更改”;,在Internet Explorer中从下拉列表中选择时?

Html 如何使用FireEvent“触发网页更新”;“一旦更改”;,在Internet Explorer中从下拉列表中选择时?,html,excel,vba,onchange,Html,Excel,Vba,Onchange,我正在创建一个宏来从公共银行网站获取传真号码 我已经编写了足够的代码来访问站点,从下拉列表中选择,并在下拉列表中更改选择。但是,当我使用FireEvent(“onChange”)时,它不会触发网页更新 我一直在寻找答案,但没有找到 网站: Public声明PtrSafe子睡眠库“kernel32”(ByVal dwr作为LongPtr) 子测试() Dim ieExplorer作为新的InternetExplorerMedium 作为对象的场 作为对象提交 选择作为对象 将按钮作为对象 将计数器

我正在创建一个宏来从公共银行网站获取传真号码

我已经编写了足够的代码来访问站点,从下拉列表中选择,并在下拉列表中更改选择。但是,当我使用FireEvent(“onChange”)时,它不会触发网页更新

我一直在寻找答案,但没有找到

网站:

Public声明PtrSafe子睡眠库“kernel32”(ByVal dwr作为LongPtr)
子测试()
Dim ieExplorer作为新的InternetExplorerMedium
作为对象的场
作为对象提交
选择作为对象
将按钮作为对象
将计数器设置为整数
作为对象的模糊对象
Dim objCount为整数
作为对象的形状
Dim intRow为长,faxNum为字符串
intRow=2
使用ieExplorer
.Visible=True
.导航“https://www.atb.com/contact-us/Pages/branch-locator.aspx"
睡1000
睡1000
睡1000
睡1000
睡1000
睡1000
睡1000
设置ieSelect=.Document.getElementsByTagName(“选择”)
选择长度时执行此操作
如果选择(o).ID=“ba”,则
对于ieSelect(o).选项中的每个i
如果i.值为“null”,则
ieSelect(o).聚焦
i、 所选=真
ieSelect(o).FireEvent“onchange”
Set ieField=.Document.getElementsByTagName(“p”)
当x
看来选择更改是由以下代码设置的:

 $('body').find('#ba').change(function(){
        var a = $(this).val();
        lookyloo(a);
    });
您应该能够使用
ExecScript
调用
lookyloo
,并传入值

例如:

测试:

Dim ie As InternetExplorer, el
Set ie = New InternetExplorerMedium
ie.Visible = True

ie.navigate "https://www.atb.com/contact-us/Pages/branch-locator.aspx"

Set el = ie.document.getElementById("ba") 'I put a break here while the page loaded...

el.selectedIndex = 5 'for example

ie.document.parentWindow.Window.execScript "lookyloo('" & el.Value & "');"

我将使用XMLHTTP/WinHttp POST请求并获取xml,然后解析它。你可以作为一个函数进行调整。我更愿意一次拿到所有的传真号码,然后把它们写在纸上。我使用xpath检索标题(分支名称)和传真号码


您可以调整xpath语法来检索列出的任何值。例如,返回的行,您可以从中选择值:

<z:row ows_ID='1' ows_Title='Acadia Valley' ows_Transit='1.00000000000000' ows_Classification='Agency' ows_Address='Acadia Valley' ows_City='Acadia Valley' ows_Postal='T0J 0A0' ows_Phone='(403) 972-3805' ows_Fax='(403) 972-2263' ows_Hours='Mon-Fri 9:00-12:30, 13:30-16:00' ows_LAT='51.159888' ows_LONG='-110.209308' ows__ModerationStatus='0' ows__Level='1' ows_UniqueId='1;#{2973F9AC-2019-4BD1-A740-41A270BAC267}' ows_owshiddenversion='3' ows_FSObjType='1;#0' ows_Created='2015-11-18 13:58:48' ows_PermMask='0x1000030041' ows_Modified='2016-02-08 11:16:05' ows_FileRef='1;#Lists/Branches/1_.000' ows_MetaInfo='1;#' />


VBA:

选项显式
公共子目录编号()
Dim body作为字符串,xmlDoc作为对象,request作为对象
Application.ScreenUpdating=False
将xmlDoc=CreateObject(“MSXML2.DOMDocument”)设置为新的MSXML2.DOMDocument60
body=“”
主体=主体和“分支”
body=body&“
body=body&“
body=body&“
body=body&“0”
body=body&“
Set request=CreateObject(“WinHttp.WinHttpRequest.5.1”)
请求
.打开“POST”https://www.atb.com/_vti_bin/lists.asmx”“错
.setRequestHeader“如果修改自”,“2000年1月1日星期六00:00:00 GMT”
.setRequestHeader“用户代理”、“Mozilla/5.0(Windows NT 6.3;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/66.0.3359.181 Safari/537.36”
.setRequestHeader“内容类型”、“文本/xml”
.发送正文
使用xmlDoc
.validateOnParse=True
.setProperty“SelectionLanguage”、“XPath”
.async=False
如果不是.LoadXML(request.responseText),那么
Err.Raise.parseError.ErrorCode、.parseError.reason
如果结束
以
以
将元素变暗为对象,计数器变长,行数变长
Set elements=xmlDoc.SelectNodes(“/@ows\u Title |/@ows\u Fax”)
rowNum=1
对于计数器=0到元素。长度-1步骤2
使用此工作簿。工作表(“表1”)
.Cells(rowNum,1)=元素(计数器)。文本
.Cells(rowNum,2)=元素(计数器+1)。文本
以
rowNum=rowNum+1
下一个
Application.ScreenUpdating=True
端接头

结果样本:


我遇到了一个类似的问题,通过将“onchange”更改为(“onchange”)来解决它。

为什么需要
onchange
事件?如果选择下拉选项,网页几乎会立即更新。
onchange
应该检测到这种变化吗?您好,当我用鼠标手动执行此操作时,您是对的,站点会立即发生变化。然而,当通过VBA编码时,它只是从下拉列表中选择分支,但不会触发网页更改。这就是我尝试使用OnChange事件的原因,因为这在其他网站上对我很有效。然而,这一个,它没有做任何事情。必须有某种形式的事件需要触发才能更改网页。在这种情况下,Onchange不起作用。嗨,Tim,非常感谢您的回复!你介意给我举个例子说明你的意思吗?不知道什么是lookyloo。它会像execscript(lookyloo(i))一样吗?
lookyloo
只是作者在页面中定义的一个javascript函数,用于管理响应select中更改的过程。你可以直接从VBA调用它,而不是试图通过触发更改事件来调用它。蒂姆,你就是那个人!经过一些重新安排,我终于成功了!非常感谢你。请问您在哪里/如何找到lookyloo功能?我试着在IE上查看inspect元素选项,甚至还查看了源代码,但找不到这个。您介意告诉我您是如何做到这一点的吗?在Chrome中加载页面>>右键单击“选择位置”下拉列表,然后在中选择“检查”>>
<z:row ows_ID='1' ows_Title='Acadia Valley' ows_Transit='1.00000000000000' ows_Classification='Agency' ows_Address='Acadia Valley' ows_City='Acadia Valley' ows_Postal='T0J 0A0' ows_Phone='(403) 972-3805' ows_Fax='(403) 972-2263' ows_Hours='Mon-Fri 9:00-12:30, 13:30-16:00' ows_LAT='51.159888' ows_LONG='-110.209308' ows__ModerationStatus='0' ows__Level='1' ows_UniqueId='1;#{2973F9AC-2019-4BD1-A740-41A270BAC267}' ows_owshiddenversion='3' ows_FSObjType='1;#0' ows_Created='2015-11-18 13:58:48' ows_PermMask='0x1000030041' ows_Modified='2016-02-08 11:16:05' ows_FileRef='1;#Lists/Branches/1_.000' ows_MetaInfo='1;#' />
Option Explicit
Public Sub GetFaxNumbers()
    Dim body As String, xmlDoc As Object, request As Object

    Application.ScreenUpdating = False
    Set xmlDoc = CreateObject("MSXML2.DOMDocument") 'New MSXML2.DOMDocument60

    body = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:soap='http://schemas.microsoft.com/sharepoint/soap/'>"
    body = body & "<soapenv:Body><GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'><listName>Branches</listName>"
    body = body & "<viewFields><ViewFields><FieldRef Name='ID' /><FieldRef Name='Title' /><FieldRef Name='Transit' />"
    body = body & "<FieldRef Name='Classification' /><FieldRef Name='Address' /><FieldRef Name='City' /><FieldRef Name='Postal' />"
    body = body & "<FieldRef Name='Phone' /><FieldRef Name='Fax' /><FieldRef Name='Hours' /><FieldRef Name='LAT' /><FieldRef Name='LONG' />"
    body = body & "</ViewFields></viewFields><rowLimit>0</rowLimit><query><Query><OrderBy><FieldRef Name='Title' Ascending='True' />"
    body = body & "</OrderBy></Query></query></GetListItems></soapenv:Body></soapenv:Envelope>"

    Set request = CreateObject("WinHttp.WinHttpRequest.5.1")
    With request
        .Open "POST", "https://www.atb.com/_vti_bin/lists.asmx", False
        .setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
        .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
        .setRequestHeader "Content-Type", "text/xml"
        .send body
        With xmlDoc
            .validateOnParse = True
            .setProperty "SelectionLanguage", "XPath"
            .async = False
            If Not .LoadXML(request.responseText) Then
                Err.Raise .parseError.ErrorCode, , .parseError.reason
            End If
        End With
    End With

    Dim elements As Object, counter As Long, rowNum As Long
    Set elements = xmlDoc.SelectNodes("//@ows_Title | //@ows_Fax")
    rowNum = 1
    For counter = 0 To elements.Length - 1 Step 2
        With ThisWorkbook.Worksheets("Sheet1")
            .Cells(rowNum, 1) = elements(counter).Text
            .Cells(rowNum, 2) = elements(counter + 1).Text
        End With
        rowNum = rowNum + 1
    Next
    Application.ScreenUpdating = True
End Sub