硒webdriver cookie使用

时间:2018-07-09 07:12:39

标签: vb.net selenium selenium-webdriver selenium-chromedriver

这是将代码从c#转换为vb.net,我在Internet上找到了它。 我想做的是我使用chrome webdriver测试一些网站,但是此页面 需要先登录。 我想将cookie存储到文本文件中,并在登录网站时恢复,因此我可以跳过登录过程,这将大大减少测试时间。 我在Google中找不到与vb.net相关的硒cookie。 所以我将跟随代码从C#转换为vb.net,但是此代码也有错误。 当访问此URL时 driver.Navigate.GoToUrl(“ https://www.naver.com”) 如果有人知道如何保存和恢复cookie,请分享或指导我。 非常感谢你

Public Class CoreDriver : Inherits Form1
    Public driver As IWebDriver
    Public Property my_name As String
    Public Property my_port As Integer
    Public default_profile_dir As String = "C:\Users\admin\AppData\Local\Google\Chrome\"
    Public chromedriver_path As String = "C:\Users\admin\Downloads\chromedriver_win32\"
    Public site_profile_path As String
    Public site_profile_path_s As String
    Public default_path As String

    Public Sub Initialize()
        Dim options As ChromeOptions = New ChromeOptions()
        options.AddArgument("--log-level=3")
        options.AddArgument("--test-type")
        'options.AddArgument("--silent")
        options.AddArgument("user-data-dir=" & site_profile_path_s)
        options.AddArgument("--disable-plugins")
        Dim driverService = ChromeDriverService.CreateDefaultService(Application.StartupPath & "\")
        driverService.HideCommandPromptWindow = True
        driverService.Port = my_port
        driver = New ChromeDriver(driverService, options)
        'driver.Manage().Timeouts().ImplicitlyWait(New TimeSpan(0, 0, 14))
        'driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(15))
        'Dim jscript As IJavascriptExecutor = TryCast(driver, IJavascriptExecutor)
        'jscript.ExecuteScript("return window.stop")

        driver.Navigate.GoToUrl("https://www.naver.com")
        Dim urlVal As String = "https://nid.naver.com/nidlogin.login?mode=form&url=https://blog.naver.com/smrush"
        driver.Navigate().GoToUrl(urlVal)
        driver.FindElement(By.Name("id")).SendKeys("id")
        driver.FindElement(By.Name("pw")).SendKeys("password")
        driver.FindElement(By.Name("frmNIDLogin")).Submit()


    End Sub

    Public Sub ConfigureProfile()
        site_profile_path_s = default_profile_dir & "profile " & my_name
        site_profile_path = site_profile_path_s & "\Default"
        default_path = default_profile_dir & "User Data\Default"

        If Not Directory.Exists(site_profile_path) Then
            CreateBlankProfile()
        Else
            CopyProfileFiles()
            RemoveOpenedTabsFiles()
        End If
    End Sub

    Public Sub CleanUpOldProfiles()
        Dim di As DirectoryInfo = New DirectoryInfo(default_profile_dir)
        Dim directories As DirectoryInfo() = di.GetDirectories("profile*", SearchOption.TopDirectoryOnly)

        If directories.Count() > 0 Then

            For Each folder In directories

                Try
                    Directory.Delete(folder.FullName, True)
                Catch
                End Try
            Next
        End If
    End Sub

    Public Sub CreateBlankProfile()
        CreateIfMissing()
        CopyProfileFiles()
        CopyProfileFolders()
    End Sub

    Public Sub CopyProfileFiles()
        Dim di As DirectoryInfo = New DirectoryInfo(default_path)
        Dim file_lib As List(Of String) = New List(Of String)() From {
            "Cookies",
            "Login",
            "Preferences",
            "Secur"
        }
        Dim files As FileInfo() = di.GetFiles("*", SearchOption.TopDirectoryOnly)

        If files.Count() > 0 Then

            For Each file In files

                If PassFileOrFolder(file.Name, file_lib) Then
                    file.CopyTo(site_profile_path & "\" & file.Name, True)
                End If
            Next
        End If
    End Sub

    Public Sub RemoveOpenedTabsFiles()
        Dim di As DirectoryInfo = New DirectoryInfo(site_profile_path)
        Dim file_lib As List(Of String) = New List(Of String)() From {
            "Current",
            "Last"
        }
        Dim files As FileInfo() = di.GetFiles("*", SearchOption.TopDirectoryOnly)

        If files.Count() > 0 Then

            For Each f In files

                If PassFileOrFolder(f.Name, file_lib) Then
                    File.Delete(f.FullName)
                End If
            Next
        End If
    End Sub

    Public Sub CopyProfileFolders()
        Dim di As DirectoryInfo = New DirectoryInfo(default_path)
        Dim folder_lib As List(Of String) = New List(Of String)() From {
            "databases",
            "Extension",
            " Storage",
            "Web Applications",
            "File System",
            "IndexedDB"
        }
        Dim directories As DirectoryInfo() = di.GetDirectories("*", SearchOption.TopDirectoryOnly)

        If directories.Count() > 0 Then

            For Each folder In directories

                If PassFileOrFolder(folder.Name, folder_lib) Then
                    DirectoryCopy(folder.FullName, site_profile_path & "\" & folder.Name, True)
                End If
            Next
        End If
    End Sub

    Private Sub CreateIfMissing()
        Directory.CreateDirectory(site_profile_path)
    End Sub

    Private Shared Sub DirectoryCopy(ByVal sourceDirName As String, ByVal destDirName As String, ByVal copySubDirs As Boolean)
        Dim dir As DirectoryInfo = New DirectoryInfo(sourceDirName)
        Dim dirs As DirectoryInfo() = dir.GetDirectories()

        If Not dir.Exists Then
            Throw New DirectoryNotFoundException("Source directory does not exist or could not be found: " & sourceDirName)
        End If

        If Not Directory.Exists(destDirName) Then
            Directory.CreateDirectory(destDirName)
        End If

        Dim files As FileInfo() = dir.GetFiles()

        For Each file As FileInfo In files
            Dim temppath As String = Path.Combine(destDirName, file.Name)
            file.CopyTo(temppath, False)
        Next

        If copySubDirs Then

            For Each subdir As DirectoryInfo In dirs
                Dim temppath As String = Path.Combine(destDirName, subdir.Name)
                DirectoryCopy(subdir.FullName, temppath, copySubDirs)
            Next
        End If
    End Sub

    Public Function PassFileOrFolder(ByVal input As String, ByVal library As List(Of String)) As Boolean
        For Each name As String In library

            If input.Contains(name) Then
                Return True
            End If
        Next

        Return False
    End Function
End Class

0 个答案:

没有答案