使用Selenium Chrome驱动程序下载.xml文件

时间:2020-05-12 21:32:06

标签: powershell selenium-webdriver selenium-chromedriver

我正在尝试使用Selenium chrome驱动程序下载xml文件,但是却提示输入:

“此类型的文件可能会损坏您的计算机。是否仍要保留该文件?”。

使用Google版本80.0.3987.122(正式版本)(32位)和ChromeDriver 80.0.3987.106。 我正在使用的powershell chrome选项如下:

$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeOptions.AddArguments(@(
    "--disable-extensions",
    "--ignore-certificate-errors"))

$download = "C:\temp\download"
$ChromeOptions.AddUserProfilePreference("safebrowsing.enabled", "true");
$ChromeOptions.AddUserProfilePreference("download.default_directory", $download);
$ChromeOptions.AddUserProfilePreference("download.prompt_for_download", "false");
$ChromeOptions.AddUserProfilePreference("download.directory_upgrade", "true");

$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($chromeOptions)

我希望能使用正确的选项删除提示。

1 个答案:

答案 0 :(得分:0)

为什么? 只需使用内置的PowerShell cmdlet即可执行此操作。 调用WebRequest,Start-BitTransfer或.Net命名空间。

您不需要浏览器即可抓取网站或下载文件。 注意事项:无论您尝试使用哪种工具,某些网站都会阻止任何自动工作。

3 ways to download files with PowerShell

# 1. Invoke-WebRequest

$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date

Invoke-WebRequest -Uri $url -OutFile $output
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"


# 2. System.Net.WebClient

$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
#OR
(New-Object System.Net.WebClient).DownloadFile($url, $output)

Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"


# 3. Start-BitsTransfer

$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date

Import-Module BitsTransfer
Start-BitsTransfer -Source $url -Destination $output

#OR
Start-BitsTransfer -Source $url -Destination $output -Asynchronous

Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"


# Get specifics for a module, cmdlet, or function
(Get-Command -Name Invoke-WebRequest).Parameters
(Get-Command -Name Invoke-WebRequest).Parameters.Keys
Get-help -Name Invoke-WebRequest -Examples
<#
# Built-In Examples

$R = Invoke-WebRequest -URI http://www.bing.com?q=how+many+feet+in+a+mile
$R.AllElements | where {$_.innerhtml -like "*=*"} | Sort { $_.InnerHtml.Length } | Select InnerText -First 5
shortest HTML value often helps you find the most specific element that matches that text.
$R=Invoke-WebRequest http://www.facebook.com/login.php -SessionVariable fb
$FB
$Form = $R.Forms[0]
$Form | Format-List
$Form.fields
$Form.Fields["email"]="User01@Fabrikam.com"
$R=Invoke-WebRequest -Uri ("https://www.facebook.com" + $Form.Action) -WebSession $FB -Method POST -Body $Form.Fields
# Sends a sign-in request by running the Invoke-WebRequest cmdlet. The command specifies a value of "fb" for the SessionVariable parameter, and saves the 
$R.StatusDescription
(Invoke-WebRequest -Uri "http://msdn.microsoft.com/en-us/library/aa973757(v=vs.85).aspx").Links.Href
#>
Get-help -Name Invoke-WebRequest -Full
Get-help -Name Invoke-WebRequest -Online



(Get-Command -Name Start-BitsTransfer).Parameters
(Get-Command -Name Start-BitsTransfer).Parameters.Keys
Get-help -Name Start-BitsTransfer -Examples
<#
# Built-In Examples

Start-BitsTransfer -Source "http://server01/servertestdir/testfile1.txt" -Destination "c:\clienttestdir\testfile1.txt"
Import-CSV filelist.txt | Start-BitsTransfer
Start-BitsTransfer -Source "c:\clienttestdir\testfile1.txt" -Destination "http://server01/servertestdir/testfile1.txt" -TransferType Upload
Start-BitsTransfer -Source "http://server01/servertestdir/testfile1.txt", "http://server01/servertestdir/testfile2.txt" -Destination 
$Cred = Get-Credential
 Start-BitsTransfer -DisplayName MyJob -Credential $Cred -Source "http://server01/servertestdir/testfile1.txt" -Destination "c:\clienttestdir\testfile1.txt"
Import-CSV filelist.txt | Start-BitsTransfer -Asynchronous -Priority Normal
Start-BitsTransfer -Source "http://server01/servertestdir/*.*" -Destination "c:\clienttestdir\"
Import-CSV filelist.txt | Start-BitsTransfer -TransferType Upload
Start-BitsTransfer -Source .\Patch0416.msu -Destination $env:temp\Patch0416.msu -ProxyUsage Override -ProxyList BitsProxy:8080 -ProxyCredential 
#>
Get-help -Name Start-BitsTransfer -Full
Get-help -Name Start-BitsTransfer -Online


<#
WebClient Class

Definition 
Namespace: System.Net
Assembly:  System.Net.WebClient.dll

Provides common methods for sending data to and receiving data from a resource identified by a URI.

https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient?view=netcore-3.1



WebClient.DownloadFile Method

Namespace: System.Net
Assembly:  System.Net.WebClient.dll

Downloads the resource with the specified URI to a local file.

https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile?view=netcore-3.1
#>

尽管如果您确实想通过Chrome使用Selenium进行此操作,那么类似的SO线程也应该有所帮助。

How to control the download of files with Selenium + Python bindings in Chrome

以及本文和示例:

PowerShell & Selenium: Automate Web Browser Interactions – Part II

关于此警告...

>“这种类型的文件可能会损害您的计算机。是否仍要保留该文件?”。

...这不是PowerShell或PowerShell警告,这是Windows和浏览器(IE,Edge,Chrome等)警告您潜在的威胁。

Working around the Google Chrome "This type of file can harm your computer" problem

Google最近宣布决定做出改进 防止在Chrome浏览器中下载不需要的软件 和Google搜索。

有关此主题,请参见这些SO线程。

How to disable 'This type of file can harm your computer' pop up

This type of file can harm your computer, trying to download an .ini file in Chrome using c# and selenium