Dicas de melhoria no código abaixo.

Marcos853

24-09-2019 19:35:27

Boa tarde!

Pessoal, vocês teriam alguma dica de como melhorar o código abaixo?

Sou leigo e estou estudando VBA, aceito qualquer sugestão no assunto!

Código: Selecionar todos

Sub Cotação()

Dim MyDate
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = False
.NAVIGATE ("https://servicos.gollog.com.br/Home/Inicio#cotacao")
While .Busy Or .ReadyState <> 4:
Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 1)
DoEvents:
Wend
    
    IE.document.getElementById("select_data").Value = Range("F8")
    IE.document.getElementById("input_cotacao").Value = Range("C6")
    IE.document.getElementById("input_destino").Value = Range("C22")
    IE.document.getElementById("input_cotacao_2").Value = ("1")
    IE.document.getElementById("input_destino_1").Value = Range("C8")
    IE.document.getElementById("select_produto").Value = Range("F6")
    IE.document.getElementById("input_entrega").Click
          
         Set target = Range("G15")
            If target.Value = "SIM" Then
            IE.document.getElementById("input_valor_nota").Value = Range("F15")
            IE.document.getElementById("input_seguro").Click
        End If
            IE.document.getElementById("quote").Click
    
While .Busy Or .ReadyState <> 4:
Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 1)
DoEvents:
Wend

.Visible = True

          On Error GoTo Invalido
          
        IE.document.getElementById("input_name").Value = ("Marcos")
        IE.document.getElementById("input_phone").Value = ("(99) 9999-99999")
        IE.document.getElementById("input_email").Value = ("marcos.abreu@spharmu.com.br")
        IE.document.getElementById("input_company").Value = ("SPecial Pharmus")
        
Dim objElementCol As Object
Dim objElement As Object

            Set objElementCol = IE.document.getElementsByTagName("input")

            For Each objElement In objElementCol
            If objElement.Value = "Cadastrar" Then

            objElement.Click


Exit For
End If
Next objElement
      

End With



Invalido:

MsgBox "Valor disponível"


End Sub

webmaster

24-10-2019 13:51:34

Marcos,

Não tem código ruim se ele resolve o problema. Melhorar é uma questão de experiência. Quanto mais você escreve, melhor ele fica.

O melhor a fazer então é codificar o máximo que puder :)

Julio Mangilli

25-10-2019 13:49:49

Olá Amigo,

O Site está com problema mas aproveitando coloquei o codigo para rodar no Chrome, se der erro tente mais de uma vez....

Sub CotaçãoGOL()

Dim driver As New Selenium.ChromeDriver
Set driver = New ChromeDriver
'driver.AddArgument ("--headless")
Dim destino As Range
Set destino = Sheets("Planilha1").Range("A1")


driver.Get "https://servicos.gollog.com.br/Home/Inicio#cotacao"


While Busy
Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 1)
DoEvents:
Wend

driver.FindElementById("select_data").SendKeys "02/10/2019"
driver.FindElementById("input_cotacao").Click
driver.FindElementById("input_cotacao").SendKeys "BRASILIA (BSB) DF"
Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 1)
driver.FindElementById("input_destino").SendKeys "BRASILIA (BSB) DF"
Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 1)


driver.FindElementById("input_cotacao_2").SendKeys "1"

Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 1)
driver.FindElementById("select_produto").Click
driver.FindElementById("select_produto").SendKeys "GDS"
Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 1)
driver.FindElementById("select_kg").SendKeys "KG"


Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 1)
driver.FindElementById("quote").Click


While Busy
Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 1)
DoEvents:
Wend




driver.FindElementById("input_name").SendKeys ("Marcos")
driver.FindElementById("input_phone").SendKeys ("(99) 9999-99999")
driver.FindElementById("input_email").SendKeys ("marcos.abreu@spharmu.com.br")
driver.FindElementById("input_company").SendKeys ("SPecial Pharmus")

Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 1)

Dim objElementCol As Object
Dim objElement As Object

Set objElementCol = driver.FindElementByClass("btn_usuario")

objElementCol.Click







While Busy
Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 1)
DoEvents:
Wend

Dim tabela As WebElement

Set tabela = driver.FindElementByXPath("Site com problemas") 'pegarnomedatabela


' Site ta dando erro mas quando voltar, pegar o XPATHFULL dele e joga ai dentro.

If tabela Is Nothing Then
MsgBox "Elemento não encontrado"
Else
tabela.AsTable.ToExcel destino
End If

driver.Quit



End Sub

Vamos Conversando.

Julio Mangilli

25-10-2019 13:52:04

Queria dizer que o webmaster , Grande tomas ta certo, precisamos funfar e ir mudando e colocado programação.
Deixei um começo legal ali, vamos conversando amigo.