Comando On Error goto
Enviado: Sex Jul 19, 2019 2:36 pm
Boa tarde pessoal, tudo bem ?
Estou enfrentando alguns problemas em um web scraping que estou fazendo com selenium devido à instabilidade do site, que em determinados momentos falha ocasionando erro ao procurar determinado elemento.
Gostaria que o código seguisse funcionando mesmo após reportar o erro. Entretanto, o comendo "On Error goto" não funciona dentro do loop pois ele só retorna uma única vez e caso algum outro erro ocorra novamente o processo pára. Vocês conseguem me ajudar a fazer isso funcionar no loop ? Agradeço desde já. Segue o código que estou montando:
Option Explicit
Public Sub teste()
On Error GoTo eh
Dim Driver As New ChromeDriver
'driver.AddArgument ("--headless")
Dim tabela As WebElement
Dim destino As Range
Dim x1 As Integer
x1 = 2
Dim CPF As String
Dim DATA As String
Dim x As Integer
Dim y As Integer
Dim nome As String
Dim z As Integer
Dim by As New by
Dim x2 As Integer
Dim clinica As String
Driver.AddArgument "--disable-plugins-discovery"
Driver.AddArgument "--disable-extensions"
Driver.AddArgument "--disable-infobars"
Driver.SetPreference "plugins.plugins_disabled", Array("Adobe Flash Player")
x = 1
Driver.Get "https://www.ans.gov.br/gpw-beneficiario ... dade.xhtml"
Driver.Timeouts.ImplicitWait = 10000
' Driver.Timeouts.Server = 120000
Do While x <> 500
z = 2
nome = Planilha3.Cells(z, 3)
Set destino = Range("A" & x1)
x = x + 1
y = 2
CPF = Planilha3.Cells(x, 1).Value
' Application.Wait Now + TimeValue("00:00:01")
DATA = Planilha3.Cells(x, 2).Value
nome = Planilha3.Cells(x, 3).Value
clinica = Planilha3.Cells(x, 4).Value
'driver.Start
'Application.Wait Now + TimeValue("00:00:01")
'Application.Wait Now + TimeValue("00:00:01")
Driver.FindElementById("form:tabSub:idCpfBene").Click
Application.Wait Now + TimeValue("00:00:01")
Driver.SendKeys (CPF)
'Application.Wait Now + TimeValue("00:00:01")
Driver.FindElementById("form:tabSub:dtNasc_input").Click
Application.Wait Now + TimeValue("00:00:01")
Driver.SendKeys (DATA)
Application.Wait Now + TimeValue("00:00:01")
Driver.FindElementById("botaoIrDadosPasso2").Click
If Driver.IsElementPresent(by.ID("botaoIrDadosPasso2")) Then
Driver.FindElementById("botaoIrDadosPasso2").Click
'Application.Wait Now + TimeValue("00:00:05")
End If
If Driver.IsElementPresent(by.ID("form:tabSub:tblPlanos_data"), timeout:=10000) Then
' Application.Wait Now + TimeValue("00:00:01")
Set tabela = Driver.FindElementById("form:tabSub:tblPlanos_data")
' Application.Wait Now + TimeValue("00:00:01")
tabela.AsTable.ToExcel destino
'Application.Wait Now + TimeValue("00:00:01")
Range("A" & x1) = nome
x2 = x1
Do While VarType(Range("B" & x2)) = 8
Range("A" & x2) = nome
Range("E" & x2) = clinica
x2 = x2 + 1
Loop
Driver.FindElementByXPath("//button[@id='form:botaoIrDadosPasso1']").Click
'Application.Wait Now + TimeValue("00:00:01")
' driver.Quit
Else
' x1 = x1 - 15
' x2 = x1 - 2
'Range("B" & x2) = "CPF Sem plano"
' Range("A" & x2) = nome
' driver.Quit
'Application.Wait Now + TimeValue("00:00:01")
Driver.FindElementByXPath("//button[@id='form:botaoIrDadosPasso1']").Click
' Application.Wait Now + TimeValue("00:00:01")
End If
eh:
x1 = WorksheetFunction.CountA(Range("B:B")) + 1
Loop
End Sub
Estou enfrentando alguns problemas em um web scraping que estou fazendo com selenium devido à instabilidade do site, que em determinados momentos falha ocasionando erro ao procurar determinado elemento.
Gostaria que o código seguisse funcionando mesmo após reportar o erro. Entretanto, o comendo "On Error goto" não funciona dentro do loop pois ele só retorna uma única vez e caso algum outro erro ocorra novamente o processo pára. Vocês conseguem me ajudar a fazer isso funcionar no loop ? Agradeço desde já. Segue o código que estou montando:
Option Explicit
Public Sub teste()
On Error GoTo eh
Dim Driver As New ChromeDriver
'driver.AddArgument ("--headless")
Dim tabela As WebElement
Dim destino As Range
Dim x1 As Integer
x1 = 2
Dim CPF As String
Dim DATA As String
Dim x As Integer
Dim y As Integer
Dim nome As String
Dim z As Integer
Dim by As New by
Dim x2 As Integer
Dim clinica As String
Driver.AddArgument "--disable-plugins-discovery"
Driver.AddArgument "--disable-extensions"
Driver.AddArgument "--disable-infobars"
Driver.SetPreference "plugins.plugins_disabled", Array("Adobe Flash Player")
x = 1
Driver.Get "https://www.ans.gov.br/gpw-beneficiario ... dade.xhtml"
Driver.Timeouts.ImplicitWait = 10000
' Driver.Timeouts.Server = 120000
Do While x <> 500
z = 2
nome = Planilha3.Cells(z, 3)
Set destino = Range("A" & x1)
x = x + 1
y = 2
CPF = Planilha3.Cells(x, 1).Value
' Application.Wait Now + TimeValue("00:00:01")
DATA = Planilha3.Cells(x, 2).Value
nome = Planilha3.Cells(x, 3).Value
clinica = Planilha3.Cells(x, 4).Value
'driver.Start
'Application.Wait Now + TimeValue("00:00:01")
'Application.Wait Now + TimeValue("00:00:01")
Driver.FindElementById("form:tabSub:idCpfBene").Click
Application.Wait Now + TimeValue("00:00:01")
Driver.SendKeys (CPF)
'Application.Wait Now + TimeValue("00:00:01")
Driver.FindElementById("form:tabSub:dtNasc_input").Click
Application.Wait Now + TimeValue("00:00:01")
Driver.SendKeys (DATA)
Application.Wait Now + TimeValue("00:00:01")
Driver.FindElementById("botaoIrDadosPasso2").Click
If Driver.IsElementPresent(by.ID("botaoIrDadosPasso2")) Then
Driver.FindElementById("botaoIrDadosPasso2").Click
'Application.Wait Now + TimeValue("00:00:05")
End If
If Driver.IsElementPresent(by.ID("form:tabSub:tblPlanos_data"), timeout:=10000) Then
' Application.Wait Now + TimeValue("00:00:01")
Set tabela = Driver.FindElementById("form:tabSub:tblPlanos_data")
' Application.Wait Now + TimeValue("00:00:01")
tabela.AsTable.ToExcel destino
'Application.Wait Now + TimeValue("00:00:01")
Range("A" & x1) = nome
x2 = x1
Do While VarType(Range("B" & x2)) = 8
Range("A" & x2) = nome
Range("E" & x2) = clinica
x2 = x2 + 1
Loop
Driver.FindElementByXPath("//button[@id='form:botaoIrDadosPasso1']").Click
'Application.Wait Now + TimeValue("00:00:01")
' driver.Quit
Else
' x1 = x1 - 15
' x2 = x1 - 2
'Range("B" & x2) = "CPF Sem plano"
' Range("A" & x2) = nome
' driver.Quit
'Application.Wait Now + TimeValue("00:00:01")
Driver.FindElementByXPath("//button[@id='form:botaoIrDadosPasso1']").Click
' Application.Wait Now + TimeValue("00:00:01")
End If
eh:
x1 = WorksheetFunction.CountA(Range("B:B")) + 1
Loop
End Sub