【问题标题】:Firefox automation using Selenium Powershell ISE, Windows 10使用 Selenium Powershell ISE、Windows 10 实现 Firefox 自动化
【发布时间】:2021-06-22 16:06:52
【问题描述】:

PS ISE 5.1 视窗 10

有人对使用 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\3.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\3.0.1\Selenium.psm1:403

字符:9 + [ValidateURIAttribute()] + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: ([ValidateURIAttribute()]:AttributeAst) [], RuntimeException + FullyQualifiedErrorId : CustomAttributeTypeNotFound 找不到自定义属性“ValidateURIAttribute”的类型。确保已加载包含此类型的程序集。 在 C:\Program Files\WindowsPowerShell\Modules\Selenium\3.0.1\Selenium.psm1:580 字符: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,

文化=中立, PublicKeyToken=30ad4fe6b2a6aeed' 或其依赖项之一。该系统找不到指定的文件。” 在 F:\Programs\PowerShell\SeleniumFireFox.ps1:15 char:18 + ... foxdriver = 新对象 OpenQA.Selenium.Firefox.Firefoxdriver($Firefox ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : InvalidOperation: ??????) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand 按 Enter 继续...:

这是在 Eclipse 中工作的 Java 代码的 sn-p。我想知道我在上面的代码中是否缺少 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 中,我无法弄清楚如何进行在 powershell 中如此简单的 API 调用;只需使用 Invoke-RestMethod。我在网上找到的答案是说它在 Java 中实际上很复杂,因为我需要管理 cookie 并做各种各样的事情。我什至找不到一致的日期函数;不同的答案是导入不同的日期功能模块,这使得组合代码变得困难。因此,我决定尝试将我的 Java 代码移植到 Powershell。我使用的代码对其他人有效,至少根据我在网上找到的答案,但不适用于我。

【问题讨论】:

    标签: windows powershell selenium firefox automation


    【解决方案1】:

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

    Import-Module Selenium
    

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

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

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

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

    【讨论】:

    • 谢谢,这解决了我的问题。问题是我导入的是 .psm1 文件而不是 .psd1 文件。我什至从未听说过 psd1,所以我认为这不是要导入的正确文件。导入psd1后,我就可以打开火狐了。
    猜你喜欢
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    相关资源
    最近更新 更多