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



PS ISE 5.1Windows 10

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

方法1)硒PS模块的安装:来源:https://github.com/adamdriscoll/selenium-powershell

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


cls
$website = "https://www.google.com/"
Import-Module "C:Program FilesWindowsPowerShellModulesSelenium3.0.1Selenium.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 FilesWindowsPowerShellModulesSelenium3.0.1Selenium.psm1:403

字符:9+[ValidateURIAttribute()]+~~~~~~~~~~~~~~~~~~~~~~~~+CategoryInfo:InvalidOperation:([ValidateURIAttribute()]:AttributeAst)[],RuntimeException+FullyQualifiedErrorId:CustomAttributeTypeNotFound找不到自定义特性"ValidateURIAttribute"的类型。请确保已加载包含此类型的程序集。位于C:\Program Files\WindowsPowerShell\Moduless\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/

在本例中,我下载了geckodoriver.exe和WebDriver.dll,将它们放在同一个文件夹中,并将文件夹添加到系统的环境变量中。我在下面添加了一个显示错误消息的评论块。我注释掉了Add类型调用,因为它给了我不同的错误,而且它没有加载网络驱动程序,所以我使用了loadform调用。错误引用的是哪个文件?该代码正在使用DLL文件;我知道这一点是因为FireFoxOptions的代码行可以工作,并且代码打开了exe,所以很明显也可以找到。只有两个文件,而且都在一定程度上起作用。不工作的线路是


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

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


cls
$PathToFolder = 'F:ProgramsSeleniumWorkingDirectory'
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:ProgramsSeleniumWorkingDirectoryWebDriver.dll                   

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

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

下面是在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链接建议如下:

Import-Module "{FullPath}selenium-powershellSelenium.psd1"

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

(Get-Module ActiveDirectory).ExportedTypeFiles
C:WINDOWSsystem32WindowsPowerShellv1.0ModulesActiveDirectoryActiveDirectory.Types.ps1xml

最新更新