使用 Selenium Powershell ISE 的 Firefox 自动化,Windows 10

Firefox automation using Selenium Powershell ISE, Windows 10

PS 伊势 5.1 Windows10

有没有人在使用 Selenium 和 PS ISE 的 Firefox 自动化方面有过好运?下面,我有两个不同的例子,在每种方法中我都无法打开 firefox 浏览器,更不用说导航到任何地方了。我会在每个方法下详细解释。

方法一)安装Selenium PS模块: 资源: https://github.com/adamdriscoll/selenium-powershell

在下面的方法中,我安装了一个模块,它应该是 C# Selenium 的 powershell 包装器。错误是由模块本身触发的。我已将错误消息放在下面的评论区中。我如何找到需要哪个包含此“类型”的“程序集”?


    cls
    $website = "https://www.google.com/"
    Import-Module "C:\Program Files\WindowsPowerShell\Modules\Selenium.0.1\Selenium.psm1" -Function *
    $Driver = Start-SeFirefox
    Enter-SeUrl $website -Driver $Driver
    
Cannot find the type for custom attribute 'ValidateURIAttribute'. Make sure that the assembly that contains this type is loaded.
At C:\Program Files\WindowsPowerShell\Modules\Selenium.0.1\Selenium.psm1:403

char:9 + [ValidateURIAttribute()] + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: ([ValidateURIAttribute()]:AttributeAst) [], RuntimeException + FullyQualifiedErrorId : CustomAttributeTypeNotFound Cannot find the type for custom attribute 'ValidateURIAttribute'. Make sure that the assembly that contains this type is loaded. At C:\Program Files\WindowsPowerShell\Modules\Selenium.0.1\Selenium.psm1:580 char:9 + [ValidateURIAttribute()] + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: ([ValidateURIAttribute()]:AttributeAst) [], RuntimeException + FullyQualifiedErrorId : CustomAttributeTypeNotFound

方法 2) 下载 geckodriver.exe 和 WebDriver.dll 资料来源:

https://www.reddit.com/.../getting_started_in_web.../

https://adamtheautomator.com/selenium-powershell/

https://www.selenium.dev/downloads/

在这个例子中,我下载了 geckodriver.exe 和 WebDriver.dll,将它们放在同一个文件夹中,并将该文件夹添加到我的系统环境变量中。我在下面添加了一个评论块来显示错误消息。我注释掉了 Add-type 调用,因为它给了我不同的错误并且它没有加载 webdriver,所以我改用了 loadform 调用。错误引用了哪个文件?代码使用的是DLL文件;我知道这是因为 FireFoxOptions 代码行有效并且代码打开了 exe,因此也清楚地找到了。只有 2 个文件,两者都在某种程度上起作用。行不通的是


    $Firefoxdriver = New-Object OpenQA.Selenium.Firefox.Firefoxdriver($Firefoxoptions)

这将是 dll 文件中的函数;即使代码已经执行了 EXE 文件,此时是否找不到 EXE?关于 GAC = false,我读到并不是所有的 DLL 都可以在系统中注册,因为有些不具备所有必要的功能;我不确定是否需要这样做。


    cls
    $PathToFolder = 'F:\Programs\Selenium\WorkingDirectory'
    if ($env:Path -notcontains ";$PathToFolder" ) {
        $env:Path += ";$PathToFolder"
    }
    [System.Reflection.Assembly]::LoadFrom("{0}\WebDriver.dll" -f $PathToFolder)
    #Add-Type -Path "$($PathToFolder)\WebDriver.dll"
    $Firefoxoptions = New-Object OpenQA.Selenium.Firefox.Firefoxoptions
    #$Firefoxoptions.AddArgument('-headless') ##### <----- Used to make the window not appear, or 'headless' - comment out to have a normal window show.
    $Firefoxoptions.AcceptInsecureCertificates = $True
    $Firefoxdriver = New-Object OpenQA.Selenium.Firefox.Firefoxdriver($Firefoxoptions)
    pause
GAC    Version        Location                                                                                                                                                
---    -------        --------                                                                                                                                                
False  v4.0.30319     F:\Programs\Selenium\WorkingDirectory\WebDriver.dll                   


New-Object : Exception calling ".ctor" with "1" argument(s): "Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0,

Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified." At F:\Programs\PowerShell\SeleniumFireFox.ps1:15 char:18 + ... foxdriver = New-Object OpenQA.Selenium.Firefox.Firefoxdriver($Firefox ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: ) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand Press Enter to continue...:

这是在 Eclipse 中运行的 Java 代码片段。我想知道我在上面的代码中遗漏的是 System.setProperty 在 Java 中所做的任何事情吗?

    public static void main(String[] args) {
     System.setProperty("webdriver.gecko.driver","F:\Programs\Selenium\GeckoDriver\geckodriver-v0.29.0-win64\geckodriver.exe");
     WebDriver driver = new FirefoxDriver();
     String baseUrl = "https://www.google.com/";
             String expectedTitle = "Google";
             String actualTitle = "";
             // launch Fire fox and direct it to the Base URL
             driver.get(baseUrl);

附加说明(部分只是发泄):我尝试在powershell中这样做的原因是因为我更了解powershell;我通常更容易编写代码。在我的示例中,我试图将两段工作代码组合在一起;其中之一是 Java 中的 Firefox 自动化,另一个是 Powershell 中的 api 调用。我的第一次尝试是将 powershell 代码移植到 Java,但我对 Java 不是很了解,而且最细微的事情都让我感到沮丧。在 Java 中,我无法弄清楚如何进行 API 调用,这在 powershell 中是如此简单;只需使用 Invoke-RestMethod。我在网上找到的答案是说它在 Java 中实际上很复杂,因为我需要管理 cookie 并做各种事情。我什至找不到一致的日期函数;不同的答案是导入不同的日期功能模块,这使得组合代码变得困难。因此,我决定尝试将我的 Java 代码移植到 Powershell。我使用的代码对其他人有效,至少根据我在网上找到的答案是这样,但对我不起作用。

您似乎没有导入完整模块,这是故意的吗?如果 Selenium 在你的模块路径中,你应该能够 运行:

Import-Module Selenium

如果那不可能,大多数模块都有一个 .psd1 文件应该是导入目标,并且可以加载其他 .psm1 文件、嵌套模块、程序集等。您的 github link 推荐如下:

Import-Module "{FullPath}\selenium-powershell\Selenium.psd1"

至少,您缺少类型定义 - 这些通常包含在模块的 .dll 或 .xml 文件中,并且可以在 get-module 下导入后看到 ExportedTypeFiles:

(Get-Module ActiveDirectory).ExportedTypeFiles
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\ActiveDirectory\ActiveDirectory.Types.ps1xml