I've been working on a series of Powershell scripts that utilize the Selenium Webdriver.
Now, I am looking to incorporate some JavaScript functionality into one of them. However, I am struggling to understand how to get the syntax right.
I tried adapting C# code from a discussion on executing JavaScript using Selenium WebDriver:
Execute JavaScript using Selenium WebDriver in C#
Here's a snippet of my code:
# Specify path to Selenium drivers
$DriverPath = (get-item ".\" ).parent.parent.FullName + "\seleniumdriver\"
$files = Get-ChildItem "$DriverPath*.dll"
# Load all Selenium drivers
foreach ($file in $files) {
$FilePath = $DriverPath + $file.Name
[Reflection.Assembly]::LoadFile($FilePath) | out-null
}
# Create ChromeDriver instance
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver
# Navigate to google.com
$driver.Url = "http://www.google.com"
# Create IJavaScriptExecutor instance
$js = New-Object IJavaScriptExecutor($driver)
# Execute Javascript to retrieve current URL title
$title = $js.executeScript("return document.title")
# Output the title to cmd
write-host $title
Unfortunately, I keep encountering an error when trying to create an instance of IJavaScriptExecutor:
"New-Object : Cannot find type [IJavaScriptExecutor]: make sure the assembly containing this type is loaded."
Can someone help me identify what I might be overlooking? Is there something wrong with my code or are there additional DLL files required?
Thanks, Christian