none
login con WebBrowser se sale de la pagina web RRS feed

  • Pregunta

  • amigos, el codigo para ingresar funciona bien, pero ahora en mi navegador tengo un problema, luego de logearme en la pagina, debo comenzar a recorrer cada link que tengo en mi grilla y sacar la información correspondiente, el problema que sucede es que luego de haberse logeado y comenzar a recorrer los link se cierra la sesión de ingreso, no entiendo como o por que sucede eso en el modulo, me gustaría puedan ayudarme y ver el código que les comparto para que si alguien encuentra algo raro entre el log y los case me pueda ayudar a resolver el problema por favor. desde ya muchas gracias:

    Imports System.Data.SqlClient
    Imports System.Runtime.InteropServices
    Imports System.Text.RegularExpressions
    
    Public Class AndesIndustrial
    
        <System.Runtime.InteropServices.DllImport("user32.dll")> _
        Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As System.IntPtr
        End Function
        <System.Runtime.InteropServices.DllImport("user32.dll")> _
        Public Shared Function SetForegroundWindow(ByVal hWnd As System.IntPtr) As Boolean
        End Function
    
        Dim opc As Integer = 0
        Dim user As String = "usuario"
        Dim pass As String = "password"
        Public Conexion As SqlConnection = New SqlConnection("Server=server;Database=dabedatos;uid=usuario;pwd=contrasenia")
        Dim Flag As Boolean = False
        Dim PasoNavegacion As Integer = 0
        Dim TextoDebug As String = ""
        Dim f As Integer = 0
        Dim NumVentana As Integer = 1
        Dim CloseWindow As Boolean = False
    
        Private Sub AndesIndustrial_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            CargarGrilla()
            WebBrowser1.Navigate("http://www.andesindustrial.cl")
            cargaautomaticamente()
        End Sub
    
        Private Sub CargarGrilla()
            Dim Conexion As New SqlClient.SqlConnection(ConnectionString)
            Conexion.Open()
            Dim ds As New DataSet
            Dim sql As String = " SELECT ID,Nombre,modelo,Precio,stock,Descripcion,Proveedor,Url,CategoriaRaiz,CategoriaSubRaiz,CategoriaPadre,Categoria,Fecha FROM LinkProductos" & _
                                " ORDER BY Id, Proveedor"
            
            da.Fill(ds, "LinkProductos")
            DGVSPORT.DataSource = ds
            DGVSPORT.DataMember = "LinkProductos"
            'Codigo para ordenar alguna columnas a eleccion, dentro del datagridview
            DGVSPORT.Columns(0).Width = 50
            DGVSPORT.Columns(1).Width = 250
            DGVSPORT.Columns(2).Width = 120
            DGVSPORT.Columns(3).Width = 180
            DGVSPORT.Columns(4).Width = 180
            DGVSPORT.Columns(5).Width = 180
            DGVSPORT.Columns(6).Width = 180
            DGVSPORT.Refresh()
            Conexion.Close()
            Conexion = Nothing
            DGVSPORT.AllowUserToAddRows = False
            'banderalink = "NUEVO"
        End Sub
    
        Private Sub login(ByVal user As String, ByVal pass As String)
            Try
                Dim frmLogin As HtmlElement = WebBrowser1.Document.GetElementById("login")
    
                If frmLogin IsNot Nothing Then
                    frmLogin.Document.GetElementById("Login_user").InnerText = user
                    frmLogin.Document.GetElementById("Login_pass").InnerText = pass
                    frmLogin.InvokeMember("submit")
                End If
                
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub ingresar()
            If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
                login("usuario", "password")
            End If
        End Sub
    
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            Try
                TextoDebug = WebBrowser1.Url.ToString
                ingresar()
                VerEstadoNavegacion()
            Catch ex As Exception
    
            End Try
        End Sub
    
        Sub cargaautomaticamente()
            Dim row As DataGridViewRow = DGVSPORT.CurrentRow
            TxtLink.Text = CStr(row.Cells(7).Value)
            TxtID.Text = CStr(row.Cells(0).Value)
        End Sub
    
        Function VerEstadoNavegacion()
            Try
                Select Case PasoNavegacion
                    Case 0
                        ingresar()
                        'Guardar_File("EnvioReclamoMP.txt", "* " & Now() & "          " & " Conecta Mercado Publico y verifica estados de Navegacion (Case 0 ) ", Strings.Replace(Mid(Now(), 1, 10).Trim, "/", "-"))
                        PasoNavegacion = 1
                    Case 1
                        If WebBrowser1.DocumentText.Contains("Hola,") Then
                            WebBrowser1.Navigate(TxtLink.Text.Trim)
                            PasoNavegacion = 2
                        Else
                            PasoNavegacion = 0
                        End If
                    Case 2
    
                        If TextoDebug = TxtLINK.Text.Trim Then
                            Dim id As String = ""
                            Dim Nombre As String = ""
                            Dim Modelo As String = ""
                            Dim precio As String = ""
                            Dim Stock As String = ""
                            Dim Descripcion As String = ""
    
                            Dim Link As String = ""
                            Dim Proveedor As String = ""
                            Dim fechaingreso As Date
                            Dim Categoria As String = ""
                            Dim cuandonohayexistencia As String = ""
                            Dim Activo As String = ""
    
                            For z As Integer = f To DGVSPORT.Rows.Count - 1
                                id = DGVSPORT.Item(0, z).Value.ToString
                                Nombre = DGVSPORT.Item(1, z).Value.ToString
                                Modelo = DGVSPORT.Item(2, z).Value.ToString
                                precio = DGVSPORT.Item(4, z).Value.ToString
                                Stock = DGVSPORT.Item(5, z).Value.ToString
                                Descripcion = DGVSPORT.Item(5, z).Value.ToString
                                Proveedor = DGVSPORT.Item(6, z).Value.ToString
                                Link = DGVSPORT.Item(7, z).Value.ToString
    
                                fechaingreso = DGVSPORT.Item(12, z).Value.ToString
                                Categoria = DGVSPORT.Item(11, z).Value.ToString
                                'For Each row As DataGridViewRow In DGVTECNOGLOBAL.Rows
                                '    '''''''''Checkbox = CheckBox1.Checked
                                '    CheckBox1.Checked = Convert.ToBoolean(row.Cells("LINKACTIVO").Value)
                                'Next
    
                                DGVSPORT.SelectionMode = DataGridViewSelectionMode.FullRowSelect
                                DGVSPORT.CurrentCell = DGVSPORT.Rows(z).Cells(0)
    
                                TxtID.Text = id
                                TxtSkuProv.Text = Modelo
                                TxtNombre.Text = Nombre
                                TxtLink.Text = Link
                                TxtCostoNeto.Text = precio
                                'CheckBox1.Checked = Checkbox
                                Exit For
                            Next
                            WebBrowser1.Navigate(TxtLINK.Text)
    
                            'Ya recorrio la grilla para pasar al siguiente
                            If f > DGVSPORT.Rows.Count - 1 Then
                                If TextoDebug = (TxtLink.Text.Trim) Then
                                    f = 0
                                    PasoNavegacion = 0
                                    Conexion.Close()
                                    CargarGrilla()
                                    Me.Close()
                                End If
                            Else
                                f = f + 1
                            End If
                            PasoNavegacion = 3
                        Else
                            PasoNavegacion = 2
                        End If
    
                    Case 3
    
                        If WebBrowser1.Url.ToString.Contains("http://andesindustrial.cl/articulo/") Then
    
                            'Obtengo el Nombre del Producto
                            Try
                                Dim Nombre As String = ""
                                Nombre = WebBrowser1.DocumentText.ToString
                                TxtNombre.Text = ""
                                Dim pi, pf As Integer
                                pi = InStr(Nombre, "id=""articulosright""") + 135
                                pf = InStr(Nombre, "codigo-producto")
                                pf = pf + 1
                                Nombre = Mid(Nombre, pi, pf)
                                pf = InStr(Nombre, "codigo-producto")
                                Nombre = Mid(Nombre, 1, pf)
                                Nombre = Replace(Nombre, Chr(34), "")
                                Nombre = Replace(Nombre, "</h1>", "")
                                Nombre = Replace(Nombre, "</div>", "")
                                Nombre = Replace(Nombre, "<div class=c", "").Trim
                                Nombre = CStr(Nombre)
                                TxtNombre.Text = Nombre
                            Catch ex As Exception
    
                            End Try
    
                            'Obtengo el modelo o codigo del Producto
                            Try
                                Dim Modelo As String = ""
                                Modelo = WebBrowser1.DocumentText.ToString
                                TxtSkuProv.Text = ""
                                Dim pi, pf As Integer
                                pi = InStr(Modelo, "codigo-producto") + 80
                                pf = InStr(Modelo, "price")
                                pf = pf + 1
                                Modelo = Mid(Modelo, pi, pf)
                                pf = InStr(Modelo, "price")
                                Modelo = Mid(Modelo, 1, pf)
                                Modelo = Replace(Modelo, Chr(34), "")
                                Modelo = Replace(Modelo, "<br>", "")
                                Modelo = Replace(Modelo, "</div>", "")
                                Modelo = Replace(Modelo, "<div class=p", "").Trim
                                Modelo = CStr(Modelo)
                                TxtSkuProv.Text = Modelo
    
                            Catch ex As Exception
                                Return Nothing
                            End Try
    
                            'OBTIENE EL PRECIO DEL PRODUCTO'
                            Try
                                Dim Precio As String = ""
                                Precio = WebBrowser1.DocumentText.ToString
                                TxtCostoNeto.Text = ""
                                Dim pi, pf As Integer
                                pi = InStr(Precio, "price") + 40
                                pf = InStr(Precio, "id=""agregar")
                                pf = pf + 1
                                Precio = Mid(Precio, pi, pf)
                                pf = InStr(Precio, "agregar")
                                Precio = Mid(Precio, 1, pf)
                                Precio = Replace(Precio, Chr(34), "")
                                Precio = Replace(Precio, "&nbsp;</td>", "")
                                Precio = Replace(Precio, ".", ",").Trim
                                Precio = CDbl(Precio)
                                TxtCostoNeto.Text = Precio
                                CalculaPrecioSinIva()
                            Catch ex As Exception
    
            End Try
    
                            ''''''''''''''''''' actualizaregistro()''''''''''''''''''
                            siguienteprod()
                            PasoNavegacion = 2
                        Else
                            PasoNavegacion = 3
                        End If
                        VerEstadoNavegacion()
                End Select
            Catch ex As Exception
                'Guardar_File("EnvioReclamoMP.txt", "* " & Now() & "   --------" & "Error en los case del switch: " & ex.ToString, Strings.Replace(Mid(Now(), 1, 10).Trim, "/", "-"))
            End Try
            Return Nothing
        End Function
    
        Private Sub limpiar()
            TxtCostoNeto.Clear()
            TxtCostoBase.Clear()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
            If Not WebBrowser1.IsBusy Then
                Timer1.Stop()
            End If
        End Sub
    
        Private Sub CalculaPrecioSinIva()
            Dim Costobase As Double = TxtCostoBase.Text
            Dim Totalsiniva As Double
            'Dim z As Integer
            If TxtCostoBase.Text <= 50000 Then
                Totalsiniva = Costobase * 1.13
                'DGVINTCOMEX.Item(5, z).Style.BackColor = Color.Red
            ElseIf TxtCostoBase.Text >= 50001 And TxtCostoBase.Text < 99999 Then
                Totalsiniva = Costobase * 1.12
            ElseIf TxtCostoBase.Text >= 100000 And TxtCostoBase.Text < 200000 Then
                Totalsiniva = Costobase * 1.11
            ElseIf TxtCostoBase.Text >= 200000 And TxtCostoBase.Text < 300000 Then
                Totalsiniva = Costobase * 1.085
            ElseIf TxtCostoBase.Text >= 300000 And TxtCostoBase.Text < 400000 Then
                Totalsiniva = Costobase * 1.078
            ElseIf TxtCostoBase.Text >= 400000 And TxtCostoBase.Text < 500000 Then
                Totalsiniva = Costobase * 1.075
            ElseIf TxtCostoBase.Text >= 500000 And TxtCostoBase.Text < 600000 Then
                Totalsiniva = Costobase * 1.095
            Else
                Totalsiniva = Costobase * 1.1
            End If
            TxtCostoNeto.Text = Totalsiniva
        End Sub
    
        Function siguienteprod()
            Try
                PasoNavegacion = 2
                Flag = False
            Catch ex As Exception
                'Guardar_File("bloqueoproductos.txt", "* " & Now() & "   --------" & " Se ha producido una Excepcion en conectaMP:  " & ex.ToString, Strings.Replace(Mid(Now(), 1, 10).Trim, "/", "-"))
            End Try
            Return Nothing
        End Function
    End Class
    Que problema ven ustedes?

    • Cambiado Enrique M. Montejo viernes, 18 de noviembre de 2016 12:11 Pregunta relacionada con controles de Windows Forms.
    martes, 15 de noviembre de 2016 12:24

Todas las respuestas