我创建了几个使用Selenium Webdriver的Powershell脚本。在Powershell中使用JavascriptExecutor
现在我需要给其中的一个添加一些Javascript功能,但是我无法弄清楚我如何获得正确的语法。
尝试下面的C#代码从这个讨论转换: Execute JavaScript using Selenium WebDriver in C#
这是我的代码看起来像此刻:
# Specify path to Selenium drivers
$DriverPath = (get-item ".\").parent.parent.FullName + "\seleniumdriver\"
$files = Get-ChildItem "$DriverPath*.dll"
# Read in all the Selenium drivers
foreach ($file in $files) {
$FilePath = $DriverPath + $file.Name
[Reflection.Assembly]::LoadFile($FilePath) | out-null
}
# Create instance of ChromeDriver
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver
# Go to example page google.com
$driver.Url = "http://www.google.com"
# Create instance of IJavaScriptExecutor
$js = New-Object IJavaScriptExecutor($driver)
# Run Javascript to get current url title
$title = $js.executeScript("return document.title")
# Write titel to cmd
write-host $title
但我不断地得到创建实例时,下面的错误IJavaScriptExecutor:
“New-Object:Can not find type [IJavaScriptExecutor]:确保包含此类型的程序集已加载。”
任何人都可以找出我失踪的东西吗?它是不正确的代码?缺少额外的DLL?
BR, 基督教
因此,我最好仔细阅读如何使用Powershell实例,接口和类。 但现在你的例子使用$驱动程序来执行JavaScript按预期工作。 谢谢罗伯特,非常感谢! –