hdsouza
hdsouza

Reputation: 355

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");

Upvotes: 0

Views: 2342

Answers (1)

Brice
Brice

Reputation: 814

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")

$ChromeDriver = 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()

Upvotes: 1

Related Questions