Powershell加载dll出错:Add-Type:未能加载文件或程序集“WebDriver.dll”或它的某一个依赖项,不支持操作

r8uurelv  于 2023-06-06  发布在  Shell
关注(0)|答案(4)|浏览(582)

我想将PowerShellselenium一起使用,并从http://www.java2s.com/Code/Jar/s/Downloadseleniumremotedriver2350jar.htm下载selenium。当我试图加载其中一个dll时,我得到了错误。希望有人能帮助我。
这是我的系统信息。

OS Name:                   Microsoft Windows 7 Enterprise
OS Version:                6.1.7601 Service Pack 1 Build 7601
OS Manufacturer:           Microsoft Corporation

这是我的PowerShell信息。

PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> $psversiontable

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.18052
BuildVersion                   6.3.9421.0
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2

PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40>

这是我在尝试加载dll时得到的错误。

PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> Add-Type -Path .\WebDriver.dll
    Add-Type : Could not load file or assembly 'file:///C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll' or one of its dependencies. Operation is
    not supported. (Exception from HRESULT: 0x80131515)
    At line:1 char:1
    + Add-Type -Path .\WebDriver.dll
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Add-Type], FileLoadException
        + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

    PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFrom(".\WebDriver.dll")
    Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Users\test\WebDriver.dll' or one of its dependencies. The system
    cannot find the file specified."
    At line:1 char:1
    + [reflection.assembly]::LoadFrom(".\WebDriver.dll")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : FileNotFoundException

    PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFrom("WebDriver.dll")
    Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Users\test\WebDriver.dll' or one of its dependencies. The system
    cannot find the file specified."
    At line:1 char:1
    + [reflection.assembly]::LoadFrom("WebDriver.dll")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : FileNotFoundException

    PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFrom("C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll")
    Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll' or
    one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)"
    At line:1 char:1
    + [reflection.assembly]::LoadFrom("C:\Users\test\Downloads\selenium-dotnet-2.35.0 ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : FileLoadException

    PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40> [reflection.assembly]::LoadFile("C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40\WebDriver.dll")
    Exception calling "LoadFile" with "1" argument(s): "An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed

 in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If
    this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more
    information."
    At line:1 char:1
    + [reflection.assembly]::LoadFile("C:\Users\test\Downloads\selenium-dotnet-2.35.0 ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : NotSupportedException

    PS C:\Users\test\Downloads\selenium-dotnet-2.35.0\net40>
jum4pzuy

jum4pzuy1#

如果您从Internet下载了. DLL,则默认情况下PowerShell将不信任它们。你可以在这里做两件事之一:
1.取消阻止内容。这里有一个guide with some elaboration on the problem,但实际上你只需要右键单击下载的文件,选择属性,然后单击常规选项卡中的“解锁”。您可以使用Unblock-File cmdlet直接从powershell取消阻止内容。
1.更改您的execution policy。这也可能允许您下载的恶意脚本运行,因此要小心。
特别是审查和解锁你信任的内容似乎是更好的途径,特别是对于你的情况,因为你似乎只会这样做一次,你信任的包。

**编辑:**如果这还不能解决这个问题,我遇到的另一个这种类型的错误是当PowerShell没有与程序集相同的.NET运行时版本时。有关某些上下文,请参阅this question,其中Powershell 2.0运行.NET 2运行时,而asker需要.NET 4用于某些程序集。

你没有使用相同版本的PS,所以你的里程可能会有所不同,但我会尝试创建/编辑一个配置文件,每个链接的答案,以支持运行时的DLL您正在使用。

yiytaume

yiytaume2#

这对我很有效:(来自https://stackoverflow.com/a/19957173/107161
在文件中:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe.config

<?xml version="1.0" encoding="utf-8" ?> 
<configuration>
   <runtime>
      <loadFromRemoteSources enabled="true"/>
   </runtime>
</configuration>
wlwcrazw

wlwcrazw3#

以管理员身份运行解决了这个问题,尽管我的get-executionpolicy都是签名的

jm81lzqq

jm81lzqq4#

我所做的
1.右键单击该文件,单击属性,然后选中解锁.dll
1.关闭PS并再次重新打开
1.运行.ps1脚本

相关问题