gpt4 book ai didi

vba - 事件 IE11 发生变化

转载 作者:行者123 更新时间:2023-12-04 07:44:13 25 4
gpt4 key购买 nike

我的代码启动一个 IE 11 页面,单击一个链接,然后尝试填写两个下拉列表。第一个成功填写值 "2012"。但是第二个没有填充值 "TOYOTA"

尽管第二个下拉列表不再显示命令“select”,但它只是不显示“TOYOTA”。换句话说,它从命令“选择”变为空白。而第一个下拉菜单正确地从命令“select”到 “2012”

我认为这可能与第二个下拉菜单将显示的值取决于第一个下拉菜单的实际选择有关。并且需要一些 onchange 或 fireevent 命令。

我一直在网上搜索很多,我发现了类似的问题,我试图解决这些问题。一般来说,我尝试过两种主要的代码变体。一如既往,如果有人可以运行此代码和/或建议我,我将不胜感激。第一个是这样的:

Sub GetQuote()

Dim IE As Object

Set IE = CreateObject("InternetExplorer.Application")

IE.navigate ("website")

IE.Visible = True

Do
DoEvents
Loop Until IE.readystate = 4

Dim e
Set e = IE.document.getElementsByClassname("id name of button")(1)
e.Click

Application.Wait (Now + TimeValue("00:00:02"))

Do
DoEvents
Loop Until IE.readystate = 4


Dim z As Object
Set z = IE.document.getElementbyid("vehicleYearOfManufactureList")
z.Focus
z.SelectedIndex = 4
z.FireEvent ("onchange")

Application.Wait (Now + TimeValue("00:00:02"))

Dim y As Object
Set y = IE.document.getElementbyid("vehicleMakeList")
y.Focus
y.Value = "TOYOTA"
y.FireEvent ("onchange")


End Sub

第二个是这样的:

Sub GetQuote()

Dim IE As Object

Set IE = CreateObject("InternetExplorer.Application")

IE.navigate ("website")

IE.Visible = True

Do
DoEvents
Loop Until IE.readystate = 4

Dim e
Set e = IE.document.getElementsByClassname("sg-Btn sg-Btn--primary")(1)
e.Click

Application.Wait (Now + TimeValue("00:00:02"))

Do
DoEvents
Loop Until IE.readystate = 4


Dim z As Object

Set evt = IE.document.createEvent("HTMLEvents")

evt.initEvent "change", True, False

Set z = IE.document.getElementbyid("vehicleYearOfManufactureList")

z.SelectedIndex = 4
z.dispatchEvent evt

Application.Wait (Now + TimeValue("00:00:02"))

Dim y As Object

Set evt = IE.document.createEvent("HTMLEvents")

evt.initEvent "change", True, False

Set y = IE.document.getElementbyid("vehicleMakeList")

y.Value = "TOYOTA"
y.dispatchEvent evt

End Sub

这是我想用“TOYOTA”填充的第二个下拉列表的源代码(当我将鼠标悬停在第二个下拉列表上时我注意到的一件事是,我的光标现在没有通常的箭头显示一只手,上面有一个黑色的小圆圈,上面有一条划线,这似乎与第二个下拉列表似乎无法被选中的事实相符):

                   <option value="">Select</option><optgroup label="Common Makes" id="commonVehicleMakeOptionGroup"><option value="AUDI">Audi</option><option value="BMW">BMW</option><option value="FORD">Ford</option><option value="HOLDEN">Holden</option><option value="HONDA">Honda</option><option value="HYUNDAI">Hyundai</option><option value="KIA">Kia</option><option value="MAZDA">Mazda</option><option value="MERCEDES-BENZ">Mercedes-Benz</option><option value="MITSUBISHI">Mitsubishi</option><option value="NISSAN">Nissan</option><option value="SUBARU">Subaru</option><option value="TOYOTA">Toyota</option><option value="VOLKSWAGEN">Volkswagen</option><option disabled="disabled"></option></optgroup><optgroup label="All Makes" id="allVehicleMakeOptionGroup"><option value="ABARTH">Abarth</option><option value="ALFA ROMEO">Alfa Romeo</option><option value="ASTON MARTIN">Aston Martin</option><option value="AUDI">Audi</option><option value="BENTLEY">Bentley</option><option value="BMW">BMW</option><option value="CHERY">Chery</option><option value="CHRYSLER">Chrysler</option><option value="CITROEN">Citroen</option><option value="DODGE">Dodge</option><option value="FIAT">Fiat</option><option value="FORD">Ford</option><option value="FOTON">Foton</option><option value="GEELY">Geely</option><option value="GREAT WALL MOTORS">Great Wall Motors</option><option value="HOLDEN">Holden</option><option value="HONDA">Honda</option><option value="HSV">HSV</option><option value="HYUNDAI">Hyundai</option><option value="INFINITI">Infiniti</option><option value="ISUZU">Isuzu</option><option value="IVECO">Iveco</option><option value="JAGUAR">Jaguar</option><option value="JEEP">Jeep</option><option value="KIA">Kia</option><option value="LAND ROVER">Land Rover</option><option value="LEXUS">Lexus</option><option value="LOTUS">Lotus</option><option value="MAHINDRA">Mahindra</option><option value="MAZDA">Mazda</option><option value="MERCEDES-BENZ">Mercedes-Benz</option><option value="MINI">Mini</option><option value="MITSUBISHI">Mitsubishi</option><option value="NISSAN">Nissan</option><option value="OPEL">Opel</option><option value="PEUGEOT">Peugeot</option><option value="PORSCHE">Porsche</option><option value="PROTON">Proton</option><option value="RANGE ROVER">Range Rover</option><option value="RENAULT">Renault</option><option value="SAAB">Saab</option><option value="SKODA">Skoda</option><option value="SMART">Smart</option><option value="SSANGYONG">Ssangyong</option><option value="SUBARU">Subaru</option><option value="SUZUKI">Suzuki</option><option value="TATA">Tata</option><option value="TOYOTA">Toyota</option><option value="VOLKSWAGEN">Volkswagen</option><option value="VOLVO">Volvo</option></optgroup></select>

最佳答案

在下拉列表中选择选项的最佳方式是使用 selectedIndex属性或设置 selected = True对于 <option> 之一元素。之后,您可以触发 onchange事件自己。由于您的页面使用 AJAX 来根据您的选择初始化更多列表,因此您必须等待它完成才能继续。

我觉得无聊,决定自己造车。我只是包装了从下拉列表中选择一个选项所需的功能触发onchange事件变成SelectOption()功能。可能有更复杂的方法来等待事件完成(例如检查 ie.busyie.readystate )但我只是在设置每个选项后睡了几秒钟。

Set d = ie.document

SelectOption d, "vehicleYearOfManufactureList", "2012"
SelectOption d, "vehicleMakeList", "Toyota"
SelectOption d, "vehicleModelList", "Camry"
SelectOption d, "vehicleTransmissionList", "Auto"
SelectOption d, "vehicleNumberOfCylindersList", "4"
SelectOption d, "vehicleBodyTypeList", "4D Sedan"

' If you want to click the button when you're done...
d.getElementById("findcar").Click

MsgBox "Completed selections"

Function SelectOption(doc, strID, strText)

Dim e
Set e = doc.getElementById(strID)

If e Is Nothing Then
MsgBox "Could not find ID = " & strText
Exit Function
End If

' Select the option that matches our text...
Dim o
For Each o In e.Options
If StrComp(o.Text, strText, vbTextCompare) = 0 Then
o.Selected = True
Exit For
End If
Next

If e.SelectedIndex = 0 Then
MsgBox "Could not set value of " & strID & " to " & strText
Exit Function
End If

' Fire the onChange event...
Dim objEvent
Set objEvent = doc.createEvent("HTMLEvents")
objEvent.initEvent "change", False, True
e.dispatchEvent objEvent

' Wait a few seconds for the event to complete...
WScript.Sleep 3000

SelectOption = True

End Function

我在 Windows 脚本宿主中执行此操作,因此我有幸使用 WScript.Sleep() .您只需要将这些调用替换为 Application.Wait()用于 VBA。

关于vba - 事件 IE11 发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31955265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com