Executar meu código VBA de uma página existente (aberta) do Internet Explorer
Gostaria de executar minha macro VBA em uma página específica do Internet Explorer que já abri e conectei .
Isso ocorre porque preciso fazer login na minha conta e ignorar o código captcha primeiro. Tenho que enviar esta mensagem extremamente repetitiva para mais de algumas centenas de pessoas ao longo do ano.
O problema a seguir é que estou abrindo uma página totalmente nova e não consigo ignorar o captcha.
Dim IE As InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
IE.navigate ("website")
IE.Visible = True
End Sub
Solução
Sub TestGetIE()
Dim IE As Object
'GetIE runs the Functoin we have created below
Set IE = GetIE("website opened in IE here ")
WaitFor IE
end sub
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim retVal As Object
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
'Loop through all the opened internet explorer pages
sURL = ""
'Loops through all the pages opened on internet explorer
'Then we will tell our macro to work on that page
sURL = o.LocationURL
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o
Set GetIE = retVal
End Function
Respostas
Uma coisa que você pode fazer é DoEvents até que um determinado Elemento esteja presente na página. Execute seu código para navegar até o site e, em seguida, faça até o elemento depois que o captcha estiver presente, faça os DoEvents. O código será executado em loop até que você esteja conectado ao site. Eu gosto de usar o VBA SeleniumBasic ChromeDriver no GetHub. Isso torna o scrap da web muito mais fácil e você não precisa usar o Internet Explorer.
Com base em sua descrição, entendo que você deseja que a interação do usuário preencha o captcha e, em seguida, queira continuar a automatizar a mesma página.
Eu sugiro que você para ambas as etapas tente usar 2 módulos diferentes e use o mesmo objeto IE em ambos os módulos.
Você pode criar um objeto IE global que pode ser usado em ambos os módulos.
Então, para a parte de login, execute o primeiro módulo.
Depois disso, o usuário preencherá o captcha manualmente.
Em seguida, execute o segundo módulo para automatizar o resto da página.
Código de amostra:
Public ie As Object
Sub login()
Set ie = CreateObject("internetexplorer.application")
ie.Visible = True
ie.navigate "Your_web_site_URL_here..."
Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
ie.document.getElementById("uname").Value = "user1"
ie.document.getElementById("pswd").Value = "12345"
End Sub
Sub second_part()
ie.document.getElementById("email").Value = "[email protected]"
ie.document.getElementById("age").Value = "30"
End Sub
Código UserForm1:
Private Sub CommandButton1_Click()
Call Module1.login
End Sub
Private Sub CommandButton2_Click()
Call Module1.second_part
End Sub
Resultado:
Chamar essa função me ajudou a resolver isso
Function GetIE(sLocation As String) As Object
Dim objShell As Object, objShellWindows As Object, o As Object
Dim sURL As String
Dim retVal As Object
Set retVal = Nothing
Set objShell = CreateObject("Shell.Application")
Set objShellWindows = objShell.Windows
For Each o In objShellWindows
'Loop through all the opened internet explorer pages
sURL = ""
'Loops through all the pages opened on internet explorer
'Then we will tell our macro to work on that page
sURL = o.LocationURL
If sURL Like sLocation & "*" Then
Set retVal = o
Exit For
End If
Next o
Set GetIE = retVal
End Function