Página 1 de 1

Selenium - Esperar até que a página carregue

Enviado: Qua Jul 17, 2019 12:02 pm
por Antonioceles
Boa tarde pessoal, tudo bem ? Estou criando um código que preenche o cpf e data de nascimento da pessoa, clica em prosseguir, pega a tabela que irá aparecer e "joga" ela para a minha planilha. Após o botão "prosseguir" estou estipulando um tempo de 3 segundos até que a página abra, entretanto a página pode ficar alguns segundos aberta sem que seja necessário (pois já carregou e capturou a tabela em menos de 3 segundos) ou simplesmente fecha o navegador sem que dê tempo de pegar a tabela. Existe alguma forma de eu esperar até que a página carregue para contornar esses problemas ? Agradeço desde já. Segue o código abaixo, marquei em amarelo a parte que gostaria de esperar a página carregar:

Option Explicit

Public Sub teste()
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


x = 1


Do While x <> 2

z = 2
nome = Planilha3.Cells(z, 3)
Set destino = Range("A" & x1)
x = x + 1
y = 2
' Application.Wait Now + TimeValue("00:00:01")
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")
driver.Get "https://www.ans.gov.br/gpw-beneficiario ... dade.xhtml"
'Application.Wait Now + TimeValue("00:00:01")
driver.FindElementById("form:tabSub:idCpfBene", 2000).Click
Application.Wait Now + TimeValue("00:00:01")
driver.SendKeys (CPF)
Application.Wait Now + TimeValue("00:00:01")
driver.FindElementById("form:tabSub:dtNasc_input", 2000).Click
Application.Wait Now + TimeValue("00:00:01")
driver.SendKeys (DATA)
Application.Wait Now + TimeValue("00:00:01")
driver.FindElementById("botaoIrDadosPasso2", 2000).Click


'Application.Wait Now + TimeValue("00:00:03")



If driver.IsElementPresent(by.ID("form:tabSub:tblPlanos_data"), 1000) 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.Quit



Else
' x1 = x1 - 15
' x2 = x1 - 2
'Range("B" & x2) = "CPF Sem plano"

' Range("A" & x2) = nome
driver.Quit



End If
x1 = WorksheetFunction.CountA(Range("B:B")) + 1

Loop


End Sub