带有Selenium 4.x:Chrome选项的Powershell



尽管我需要在用户配置文件下运行chrome(它将持续存在(,但我能够成功地使用Selenium 4.x执行Powershell脚本。目的是保存cookie。为此,我相信我需要添加Saving chrome cookies Selenium中定义的选项。我确实在这里看到了类似的东西,尽管Powershell需要它。

那么,我该如何实现此命令可以使用的选项呢。(此命令主要针对selenium 4.x(。

$browser=启动SeDriver-浏览器Chrome

这是我能得到的全部,但我不确定如何将其全部相加:

$ChromeOptions.addArguments("c:/users/PsUser"(;

带模块https://github.com/adamdriscoll/selenium-powershellv4:

$browser = Start-SeDriver -Browser Chrome -Arguments "--user-data-dir=C:Users$($env:username)AppDataLocalGoogleChromeUser Data"

无模块:

实例化ChromeDriver对象时,可以传递ChromeOptions参数。所以基本上:

# 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)AppDataLocalGoogleChromeUser 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()

最新更新