gpt4 book ai didi

VBA:Excel宏代码修正

转载 作者:行者123 更新时间:2023-12-04 20:32:47 26 4
gpt4 key购买 nike

我正在寻找可以帮助我单击网页上的按钮的 VBA 代码。我从一个按预期工作的站点获得了以下 VBA。

Sub followWebsiteLink()

Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim Link As Object
Dim ElementCol As Object

Application.ScreenUpdating = False

Set ie = New InternetExplorer
ie.Visible = True

ie.navigate "https://www.google.co.in"

Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = "Loading website…"
DoEvents
Loop

Set html = ie.document
Set ElementCol = html.getElementsByTagName("a")

For Each Link In ElementCol
If Link.innerHTML = "Google Pixel 2" _ 'Or u can use "Advertising"
Then
Link.Click
End If
Next Link

Set ie = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
End Sub

但是,如果我将其替换为我想要的站点名称,它将无法正常工作。

**我所做的更改如下**
ie.navigate "https://www.flipkart.com/"


For Each Link In ElementCol
If Link.innerHTML = "Log In" _
And Link.getElementsByClassName = "_2k0gmP" _
Then
Link.Click
End If
Next Link

最佳答案

试试这个代码,为我工作点击Log In
代码

#If VBA7 Then
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Sub test()
Dim IE As Object
Dim htmlDoc As Object
Dim sURL As String

' CREATING OBJECT
Set IE = CreateObject("internetexplorer.application")

sURL = "https://www.flipkart.com/"
' WEBPAGE NAVIGATION
With IE
.navigate (sURL)
.Visible = True
End With

WaitIE IE, 2000

' CLICK ON LOG IN
Set htmlDoc = IE.document
Set buttonclick = htmlDoc.getElementsByClassName("_2k0gmP")

buttonclick(7).Click
WaitIE IE, 5000

'IE.Quit
'Set IE = Nothing
End Sub

Sub WaitIE(IE As Object, Optional time As Long = 250)
'Code from: https://stackoverflow.com/questions/33808000/run-time-error-91-object-variable-or-with-block-variable-not-set
Dim i As Long
Do
Sleep time
Debug.Print CStr(i) & vbTab & "Ready: " & CStr(IE.readyState = 4) & _
vbCrLf & vbTab & "Busy: " & CStr(IE.Busy)
i = i + 1
Loop Until IE.readyState = 4 Or Not IE.Busy
End Sub

编辑

您可以使用此代码获取 href:
For Each Element In buttonclick
Debug.Print Element.href
Next

或获取 InnerText:
For Each Element In buttonclick
Debug.Print Element.innerText & " " & i
i = i + 1
Next
'Where i is the number of the item

然后您可以使用条件,例如:
For Each Element In buttonclick
If Element.href = "href_link" Then Element.Click
Next

或者
For Each Element In buttonclick
If Element.innerText = "Log In" Then Element.Click
Next

关于VBA:Excel宏代码修正,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47333569/

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