How to start Chromium Edge selenium webdriver in application mode using powershell - powershell

I would like to start the Chromium Edge selenium webdriver in application mode using powershell.
This is how you do it from the powershell command line without the selenium webdriver:
& "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --app="https://stackoverflow.com"
This is what I've tried so far but it doesn't seem to work.
Specifically, the Chromium Edge webdriver window starts up, but not in application mode.
$optionSettings = #{
BrowserName = ''
BinaryLocation = $pathToDriver
}
$options = New-Object -TypeName OpenQA.Selenium.Chrome.ChromeOptions -Property $optionSettings
$options.addArgument("app='https://stackoverflow.com'")
$service = [OpenQA.Selenium.Chrome.ChromeDriverService]::CreateDefaultService($pathToDriver, 'msedgedriver.exe')
$driver = New-Object -TypeName OpenQA.Selenium.Chrome.ChromeDriver -ArgumentList $service,$options
I've also tried substituting the following for the addArgument:
$options.addArgument("app=https://stackoverflow.com")
$options.addArgument("--app='https://stackoverflow.com'")
$options.addArgument("--app=https://stackoverflow.com")
Any ideas?

First, if you need to start Chromium Edge via Webdriver, you need to use EdgeDriver instead of ChromeDriver. Secondly, you need to use Selenium 4 webdriver or above.
Here is a simple demo, and it works well:
[System.Reflection.Assembly]::LoadFrom("E:\Selenium\WebDriver.dll")
$options = New-Object OpenQA.Selenium.Edge.EdgeOptions
$options.addArguments("--app=https://stackoverflow.com")
#$options.AcceptInsecureCertificates = $True
$driver = New-Object OpenQA.Selenium.Edge.EdgeDriver("C:\Users\Administrator\Desktop\",$options)
#$driver.Url = "https://stackoverflow.com"
Note: Modify the path parameters according to your own situation.

Related

Powershell with Selenium 4.x: Chrome options

I am able to successfully execute a Powershell script with Selenium 4.x although i need to run chrome under a user profile (which will persist). The objective is to save the cookies. For that I believe I will need to add options as defined in Saving chrome cookies Selenium. I do see something similar here although I need it for Powershell.
So how do I implement options that can be used by this command. (This command is primarily for selenium 4.x).
$browser = Start-SeDriver -Browser Chrome
This is all I could get, but I am unsure how to add it all up:
$ChromeOptions.addArguments("c:/users/PsUser");
With Module https://github.com/adamdriscoll/selenium-powershell v4:
$browser = Start-SeDriver -Browser Chrome -Arguments "--user-data-dir=C:\Users\$($env:username)\AppData\Local\Google\Chrome\User Data"
Without Module:
When you instantiate the ChromeDriver object you can pass a ChromeOptions argument.
So basically:
# Your working directory
$workingPath = 'C:\selenium'
# Add the working directory to the environment path.
# This is required for the ChromeDriver to work.
if (($env:Path -split ';') -notcontains $workingPath) {
$env:Path += ";$workingPath"
}
# OPTION 1: Import Selenium to PowerShell using the Add-Type cmdlet.
Add-Type -Path "$($workingPath)\WebDriver.dll"
# Create a new ChromeDriver Object instance.
$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeOptions.AddArgument("--user-data-dir=C:\Users\$($env:username)\AppData\Local\Google\Chrome\User Data")
$chrome = New-Object OpenQA.Selenium.Chrome.ChromeDriver($ChromeOptions)
# Launch a browser and go to URL
$ChromeDriver.Navigate().GoToURL('https://google.com')
# Cleanup
$ChromeDriver.Close()
$ChromeDriver.Quit()

Open User Profile In Chrome using PowerShell and Selenium

So I have found little to no info on this topic, basically I'm just trying to use selenium to open up a chrome profile so that the settings are saved to be used in each run of the script. Please help as I am new to coding and have never used PowerShell.
So far the only semi successful attempt with no errors is as follows but still does not work..
$WebDriverPath = Resolve-Path "C:\selenium\WebDriver.dll"
Unblock-File $WebDriverPath
Add-Type -Path $WebDriverPath
$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$CD = $ChromeDriver.AddArgument("--user-data-dir=C:\Users\jshaw\AppData\Local\Google\Chrome\User Data\Profile 2")
$CD = New-Object OpenQA.Selenium.Chrome.ChromeDriver
$CD.Navigate().GoToURL('https://www.google.com')
So i figured out how to do this! This loads your default profile; however, there is an issue when you do this. If you have Chrome open already it throws an error. when Chrome is closed all good! Now I know why people use customized profiles.
$ChromeOptions.AddArgument(
"--user-data-dir=C:\Users\TweakAir\AppData\Local\Google\Chrome\User Data"
)
$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($ChromeOptions)
$ChromeDriver.Navigate().GoToURL($YourURL)```

Powershell, Invoke Web-Request Error (just at Windows Server 2012R)

at the moment i am writing some scripts to handle our Backups of our Costumers.
So the backups are running over a Synology Box.
The Script works really fine with Windows 10 and Windows Server 2019.
But i can not start it at WindowsServer 2012R2. And i tried so many things to handle but it didnt work.
$ip = "XXXXXXXXXX"
$u = "XXXXXXX"
$p = "XXXXXXX"
$d = "XXXXXX"
#Session ID auslesen & Authentifizierung
$a = Invoke-WebRequest -UseBasicParsing "http://$ip/webapi/auth.cgi?api=SYNO.API.Auth&version=2&method=login&account=$u&passwd=$p&session=ActiveBackup&format=cookie"
$ajson = $a.Content
$aobject = ConvertFrom-Json -InputObject $ajson
$s = $aobject.data.sid
At first he shows me the error that he can not handle utf-8, ive updated Powershell to Version 5.1 and now Windows always tells me that the Value of $a is not allowed to be null. It Seems that he cannot receive any session Number. On Windows 10 and Windows Server 2019 it runs perfectly. Please can anybody help me please. Would beeee really nice
Greetings
Yaspo
Edit i got it.
For everybody who got the same problems just go the classic way without
invoke-webrequest
$url = "http://$ip/webapi/auth.cgi?api=SYNO.API.Auth&version=2&method=login&account=$u&passwd=$p&session=ActiveBackup&format=cookie"
$wc = New-Object System.Net.WebClient
$result = $wc.DownloadString($url)

Using IE automation in powershell to login to a website that has input validation

I am trying to use PowerShell to login to a website. In the example below I am trying to login to live.com.
I am able to update the username field but the webpage runs some sort of input validation that does not accept my value. If I manually go in and edit the username field, like hitting space and then backspace, the input is then valid.
I found some documentation about changing the focus or using fireevent, but neither seems to work.
While sendkeys would resolve my issue, I have had numerous problems with sendkeys before and would really like to avoid going down that path.
$Site = 'https://login.live.com'
$UserName = 'FakeUserName#outlook.com'
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
$ie.Navigate($Site)
while ($IE.busy)
{
Start-Sleep -Milliseconds 100
}
$Inputs = $IE.document.getElementsByTagName("input")
foreach ($Input in $Inputs)
{
if ($Input.type -eq "email")
{
$UserIDField = $Input
}
if ($Input.type -eq "submit")
{
$LoginButton = $Input
}
}
$UserIDField.focus()
$UserIDField.value = $UserName
$UserIDField.FireEvent('onchange')
$LoginButton.focus()
$LoginButton.click()
#Ranadip Dutta is certainly true, you should not, do that this way, but if you want to automate web browser Selenium is a good tool, here it tooks five minutes to automate Chrome on your web site. You can chooe an IE driver,Mozilla or Opera. for that have a look to Selenium.
# Selenium directory is the place where I expand Selenium Client & WebDriver Language Bindings for C#
$seleniumDir = 'D:\Developpements\Pgdvlp_PowerShell\selenium-dotnet-3.0.0'
# Selenium Webdriver
Add-Type -Path "$seleniumDir\net40\WebDriver.dll"
Add-Type -Path "$seleniumDir\net40\WebDriver.Support.dll"
Add-Type -Path "$seleniumDir\net40\ThoughtWorks.Selenium.Core.dll"
Add-Type -Path "$seleniumDir\net40\Selenium.WebDriverBackedSelenium.dll"
# With Chrome
# I Download Chrome driver here : https://chromedriver.storage.googleapis.com/index.html?path=2.25/
# It stands in "$seleniumDir" drive
$chrome = New-Object OpenQA.Selenium.Chrome.ChromeDriver "$seleniumDir"
#$chrome.Navigate().GoToUrl("https://fr.hightail.com/loginSpaces?redirect_url=https%3A%2F%2Fspaces.hightail.com%2Foauth%2Fhightail");
$chrome.Navigate().GoToUrl("https://login.live.com");
$Browser = $chrome
$email = $Browser.FindElements([OpenQA.Selenium.By]::Name('loginfmt'))
$email[0].SendKeys("adress#hotmail.com")
$button = $Browser.FindElements([OpenQA.Selenium.By]::Id('idSIButton9'))
$button.Click()
Start-Sleep 2
$passwd = $Browser.FindElements([OpenQA.Selenium.By]::Name('passwd'))
$passwd[0].SendKeys("toto")
$button = $Browser.FindElements([OpenQA.Selenium.By]::Id('idSIButton9'))
$button.Click()
If your website is checking for automated login then how can you expect it to be automated in this way. Sendkeys actually send like user input which is similar to what user does and thereby sorts your problem in that case.
I would like you to see if there is any API available for the web service to get logged in.
Other than that, I do not see anything which can help you. This concern is not about powershell or any scripting language. It is pretty much generic for your website.
You may also want to consider passing stored credentials more securely instead of putting your creds in full view plain text within your script(s).
TechNet - PowerShell Tip - Storing and Using Password Credentials

How to modify COM+ applications from powershell

I am automating the creation of a web server. An application is created for me, but I need to manually change the Identity of a COM+ Application to run as a specific user.
Being a linux admin with little experience with powershell, I'm in over my head. It looks like there is an API to modify COM+ applications.
https://msdn.microsoft.com/en-us/library/ms679173(v=vs.85).aspx
From this stackoverflow question, I've gotten this far in modifying the application
$comAdmin = New-Object -comobject COMAdmin.COMAdminCatalog
$apps = $comAdmin.GetCollection(“Applications”)
$apps.Populate();
I am able to see my application in the list by typing in this command
$apps
Is it possible to modify the foobar application Identity from powershell?
Thanks to this stackoverflow question, I got it working.
$targetApp = "examplecompany"
$comAdmin = New-Object -comobject COMAdmin.COMAdminCatalog
$apps = $comAdmin.GetCollection("Applications")
$apps.Populate();
$app = $apps | Where-Object {$_.Name -eq $targetApp}
$comAdmin.ShutdownApplication($targetApp)
$app.Value("Identity") = 'example.com\exampleuser'
$app.Value("Password") = 'correct-horse-battery-staple'
$apps.SaveChanges()
$comAdmin.StartApplication($targetApp)