Can powershell be used to click an angularjs button? Can powershell be used to click an angularjs button? powershell powershell

Can powershell be used to click an angularjs button?


Invoke-WebRequest in AngularJS webpages is useless, since there is client side javascript rendering. You can use combination of powershell + selenium + protractor for that (Powershell v5 required):

# initialize selenium$protractorPackage = Install-Package Protractor -Destination ".\NugetPackages" -Force -Source 'https://www.nuget.org/api/v2' -ProviderName NuGetAdd-Type -Path ".\Selenium.WebDriver.$($protractorPackage.Where({$_.name -eq "Selenium.WebDriver"}).version)\lib\net40\WebDriver.dll"Add-Type -Path ".\Protractor.$($protractorPackage.Where({$_.name -eq "Protractor"}).version)\lib\net40\Protractor.dll"# initialize chrome driver$chromeDriverPackage = Install-Package Selenium.WebDriver.ChromeDriver -Destination "." -Force -Source 'https://www.nuget.org/api/v2' -ProviderName NuGet$Env:Path += ";" + ((Resolve-Path ".\Selenium.WebDriver.ChromeDriver.$($chromeDriverPackage.Version)\driver\win32") -join ";")$Selenium = New-Object OpenQA.Selenium.Chrome.ChromeDriver# interact with website$Selenium.Manage().Timeouts().SetScriptTimeout([TimeSpan]::FromSeconds(15)) # Configure timeouts (important since Protractor uses asynchronous client side scripts)$Protractor = New-Object Protractor.NgWebDriver($Selenium)try{    $Protractor.Url = "https://url.com/folder/2880"    $Protractor.WaitForAngular() # sync with angular, this waits for all elements to load    $Protractor.FindElement([OpenQA.Selenium.By]::CssSelector('[ng-click="vm.callbacks.compareAll();"]')).Click();    Write-Host "Url is: $($Protractor.Url)"    $FullhtmlDOM = $Protractor.PageSource    Write-Host "Full page source: $FullhtmlDOM"}finally{    $Protractor.Dispose()}

if you don't want to depend on Chrome browser, you can use headless PhantomJS instead. It will work the same way, you just download different packages:

...# initialize phantomjs driver$phantomJsDriverPackage = Install-Package Selenium.WebDriver.PhantomJS -Destination "." -Force -Source 'https://www.nuget.org/api/v2' -ProviderName NuGet$Env:Path += ";" + ((Resolve-Path ".\Selenium.WebDriver.PhantomJS.$($phantomJsDriverPackage.Version)\driver") -join ";")$Selenium = New-Object OpenQA.Selenium.PhantomJS.PhantomJSDriver...


If your fine with IE It appears creating an IE COM object will get what you want.

$ie = New-Object -ComObject "InternetExplorer.Application"$ie.Navigate("URL GOES HERE")$Document = $ie.document$Document.GetElementsByTagName('div')

If you want it visible:

$ie.visible = $True

Here is the Microsoft page on the COM Object

https://msdn.microsoft.com/en-us/library/aa752084(v=vs.85).aspx

And one on using it in powershell.

https://blogs.msdn.microsoft.com/luisdem/2016/02/09/browsing-in-internet-explorer-via-powershell/