如何在Visual Basic中向我的应用程序添加Chrome Web浏览器控件

时间:2019-02-12 12:17:50

标签: vb.net visual-studio browser

我想在我的应用程序中添加google chrome控件,因为Internet Explorer控件不是很好。它显示了错误的网页等。请提供一些帮助

2 个答案:

答案 0 :(得分:0)

if you want replace internet explorer with chrome in your winforms you must have to include third party tool (library). now a days there many some of that are  

    1] Selenium 
    2] CefSharp
    3] DotNet Browser
    4] OpenWebKitSharp

given list are only some browser . you could also google it out "winform replace webbrowser with chrome".

Thanks

答案 1 :(得分:0)

我的朋友请参考下面给出的代码。

   Imports Microsoft.Win32
    Imports System
    Imports System.Collections
    Imports System.ComponentModel
    Imports System.Drawing
    Imports System.Runtime.CompilerServices
    Imports System.Windows.Forms
    Imports mshtml
    Imports System.Net
    Imports System.IO
    Imports System.Text
    Imports System.Drawing.Imaging
    Imports OpenQA.Selenium.Chrome.ChromeDriver
    Imports OpenQA.Selenium
    Imports OpenQA.Selenium.Interactions
    Imports OpenQA.Selenium.Interactions.Actions
    Imports OpenQA.Selenium.Support.UI
    Imports OpenQA.Selenium.Chrome
    Imports OpenQA.Selenium.Support.Events
    Imports System.Text.RegularExpressions

Public Class CLOGIN
     Public MASTERID As String = ""
    Public MASTERPASSWORD As String = ""
    Dim webbrowse As New WebBrowser()
    Private _DOCUMENTREADY As Integer
    Private ERRORSFOUND As Boolean
    Private RequestID As String
    Private READYSTATE As Boolean
    Public mconfig As New MasterConfig()
    Public GSTCAPTCHA As New CommonCaptcha()
    Private CaptchaCookie = ""

    Private WEBSTATEINC As Boolean = True
    Public wbr As New Chrome.ChromeDriver(mconfig.MASTERSERVICE, mconfig.MASTEROPTION)
    Public WithEvents driver As New EventFiringWebDriver(wbr)







    Private Sub CLOGIN_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        AddHandler driver.Navigated, AddressOf OnNavigated
        RECALL()
    End Sub

    Public Function CropImage(ByVal source As Bitmap, ByVal section As Rectangle) As Bitmap
        Dim bmp As New Bitmap(section.Width, section.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel)
        Return bmp
    End Function
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


    End Sub
    Public Sub RECALL()
INITLABEL:
        wbr.Url = "https://example.com/services/do_login"
        wbr.Navigate()

        System.Threading.Thread.Sleep(2500)
        Application.DoEvents()

        If Not wbr.Url.Contains("/do_login") Then
            GoTo INITLABEL
        End If

        Dim origialheight = Me.MdiParent.Height
        Dim dblheight = origialheight * 0.75

        Dim proportedration = origialheight - dblheight
        Dim maxwidth = Me.MdiParent.Width

        Me.MdiParent.WindowState = FormWindowState.Normal
        Me.MdiParent.Location = New Point(0, dblheight + 1)
        Me.MdiParent.Size = New Size(maxwidth, proportedration) 'dblheight

        wbr.Manage().Window().Size = New System.Drawing.Size(maxwidth, dblheight) 'dblheight
        wbr.Manage().Window().Position = New Point(0, 0)

        Dim action As New Actions(wbr)

        Dim wait As New WebDriverWait(wbr, System.TimeSpan.FromSeconds(5)) 
        wait.Until(ExpectedConditions.ElementExists(By.Id("username")))

        wbr.FindElement(By.XPath("//*[@id='username']")).SendKeys(MASTERID) 'txtuserid.Text
        wbr.FindElement(By.XPath("//*[@id='user_pass']")).SendKeys(MASTERPASSWORD)
        Threading.Thread.Sleep(3000)
        Application.DoEvents()
        Dim master = ""

        wbr.FindElement(By.XPath("//*[@id='captcha']")).SendKeys(OpenQA.Selenium.Keys.Tab)

    End Sub

    Private Sub btnlogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogout.Click

    End Sub

    Private Sub lblshowpass_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblshowpass.CheckedChanged
        If lblshowpass.Checked Then
            txtpassword.PasswordChar = ""
        Else
            txtpassword.PasswordChar = "*"
        End If
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, Button5.Click
        wbr.FindElement(By.XPath("//*[@id='captcha']")).SendKeys(TextBox2.Text)
        wbr.FindElement(By.XPath("//*[@type='submit']")).Submit()

        WaitUntilDocumentIsReady(TimeSpan.FromSeconds(5))
        System.Threading.Thread.Sleep(1500)
        Application.DoEvents()

        If wbr.Url.Contains("/login") Then
            Dim wait As New WebDriverWait(wbr, System.TimeSpan.FromSeconds(3)) 'you can play with the time integer  to wait for longer than 15 seconds.`

            Try
                Dim myLink = wbr.FindElementByXPath("//*[@data-ng-if='loginform.captcha.$error.invalid_captcha']")
                If Not myLink Is Nothing Then 'wbr.PageSource.Contains("Enter valid Letters shown") Then
                    MsgBox("Invalid Captcha Code", MsgBoxStyle.Critical, "Application Error")
                    wbr.FindElement(By.XPath("//*[@id='user_pass']")).SendKeys(txtpassword.Text)
                    Me.TextBox1.Text = ""
                    Me.TextBox1.Focus()
                End If
            Catch ex As Exception

            End Try

            Try
                If wbr.PageSource.Contains("class=""alert alert-danger""") Then
                    MsgBox("Invalid User ID or Password", MsgBoxStyle.Critical, "Application Error")
                    wbr.Dispose()
                    Application.Exit()
                End If
            Catch ex As Exception

            End Try
        Else
            wbr.Manage().Window().Maximize()
            Me.MdiParent.WindowState = FormWindowState.Minimized
        End If
    End Sub
    Public Sub ReCaptcha(ByVal wbr As ChromeDriver)

    End Sub
    Public Sub OnNavigated(ByVal sender As Object, ByVal e As Support.Events.WebDriverNavigationEventArgs) Handles driver.Navigated
        If wbr.Url.Contains("/do_login") Then
            Dim wait As New WebDriverWait(wbr, System.TimeSpan.FromSeconds(3)) 

            Try
                Dim myLink = wbr.FindElementByXPath("//*[@data-ng-if='loginform.captcha.$error.invalid_captcha']")
                If Not myLink Is Nothing Then 'wbr.PageSource.Contains("Enter valid Letters shown") Then
                    MsgBox("Invalid Captcha Code", MsgBoxStyle.Critical, "Application Error")
                    wbr.FindElement(By.XPath("//*[@id='user_pass']")).SendKeys(txtpassword.Text)
                    Me.TextBox1.Text = ""
                    Me.TextBox1.Focus()
                End If
            Catch ex As Exception

            End Try

            Try
                If wbr.PageSource.Contains("class=""alert alert-danger""") Then
                    MsgBox("Invalid User ID or Password", MsgBoxStyle.Critical, "Application Error")
                    wbr.Dispose()
                    Application.Exit()
                End If
            Catch ex As Exception

            End Try
        Else

        End If

    End Sub






    Public Sub truncateUnfinish(ByVal string1 As String)
        If Not IsNumeric(string1) Then
            Return
        End If
        Dim answer = Regex.Replace(string1, "\D", "")
        If string1.Length > 6 Then
            TextBox2.Text = answer.Substring(0, 6)
        End If
    End Sub

    Function AllCaps(ByVal stringToCheck As String) As Boolean
        AllCaps = StrComp(stringToCheck, UCase(stringToCheck), vbBinaryCompare) = 0
    End Function
End Class

谢谢。