Página 1 de 1

[Resolvido] Selenium - Problema com IsElementPresent

Enviado: Sex Jul 12, 2019 12:01 pm
por Antonioceles
Boa tarde, estou tentando criar um código para verificar se determinados CPFs possuem plano de saúde pelo site da ANS. Entretando, caso o CPF não o possua o registro de algum plano ele me levará para uma outra página e consequentemente eu não conseguiria encontrar o elemento que procuro, portanto precisaria fechar o Driver e continuar o loop, mas não estou conseguindo usar o IsElementPresent para me retornar se o elemento existe ou não.

Alguém, por gentileza, conseguiria me ajudar com isso ? Segue o código abaixo.

Código: Selecionar todos

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 by
    

    x = 1
 

    Do While x <> 4

        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
        
        
     
        
        driver.Start
        Application.Wait Now + TimeValue("00:00:01")
        driver.Get "https://www.ans.gov.br/gpw-beneficiario/pages/pesquisa_portabilidade.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:01")
        
    
        
        If driver.IsElementPresent(by.ID("form:tabSub:tblPlanos_data"), 1000) Then
        
        
            Set tabela = driver.FindElementById("form:tabSub:tblPlanos_data")
      
        Else
        
            driver.Quit
        
        End If
        
         
        If tabela Is Nothing Then
        
            MsgBox "Elemento não encontrado"
        
        Else
        
            tabela.AsTable.ToExcel destino
        
            driver.Quit
        
            x1 = x1 + 10
    
        End If
    Loop
    

End Sub

Re: [Selenium] Problema com IsElementPresent

Enviado: Sex Jul 12, 2019 1:39 pm
por webmaster
Não entendi. O IsElementPresent não está funcionando?

Re: [Selenium] Problema com IsElementPresent

Enviado: Seg Jul 15, 2019 10:54 am
por Antonioceles
Consegui solucionar, dei uma lida em um tópico do forum parecido. O erro do findelement estava na declaração:

Dim by As by

para funcionar coloquei:

Dim by As New by

Obrigado