英文:
Default download directory Edge web driver - powershell
问题
我正在寻求使用Selenium EdgeOptions prefs功能(https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/capabilities-edge-options#edgeoptions-object)来设置下载位置。Prefs是一个字典,所以我主要是想确保语法正确,但我还没有成功。我不确定是否需要首先将prefs字典转换为PowerShell哈希表?
代码如下:
$options = New-Object OpenQA.Selenium.Edge.EdgeOptions
$options.AddAdditionalCapability('prefs', @{
'download.default_directory' = 'C:\temp'
})
$driver = New-Object OpenQA.Selenium.Edge.EdgeDriver($options)
在中间那一行有一些问题,因为我不确定如何正确更新嵌套字典“prefs”中的此值。它嵌套是因为“download”也是一个字典。
编辑:
我现在使用AddAdditionalCapability()方法,当运行脚本时不会引发任何错误。然而,下载位置仍然没有成功通过以下命令更改:
$options = New-Object OpenQA.Selenium.Edge.EdgeOptions
$options.AddAdditionalCapability('prefs', @{
'download' = @{
'default_directory' = 'C:\temp'
}
})
$driver = New-Object OpenQA.Selenium.Edge.EdgeDriver($options)
这个Python解决方案是我试图在PowerShell中实现的,我正在使用Selenium版本3。我考虑过升级到版本4,但我不确定EdgeDriver是否已经支持。我发现大多数EdgeDriver文档在方法和属性名称方面都有些不一致。
总之,我不确定这段代码出了什么问题,无论是语法问题还是其他问题。例如,这个Microsoft EdgeDriver文档说“AddAdditionalEdgeOption”方法是更改下载位置的方法。但当我调用
$options | Get-Member
我发现我认为需要使用的方法叫做“AddAdditionalCapability”。我假设这种差异是由不同的Selenium或EdgeDriver版本引起的,但我无法在任何地方确认。
无论如何,如果有人曾经使用Selenium和EdgeDriver来使用PowerShell更改默认下载位置(我假设至少有一人这样做过哈哈),我将非常感激一些帮助!
英文:
I am looking to use Selenium EdgeOptions prefs capability ( https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/capabilities-edge-options#edgeoptions-object ) to set the download location. Prefs is a dictionary so I am essentially just looking to get the syntax right, which I have not yet been able to. I am not sure if I need to turn the prefs dictionary into a PowerShell hash table first or not?
Code so far:
$options = New-Object OpenQA.Selenium.Edge.EdgeOptions
$options.AddAdditionalEdgeOption("prefs", {"download.default_directory" ; "C:\temp"})
$driver = New-Object OpenQA.Selenium.Edge.EdgeDriver($options)
Something is incorrect in the middle line as I am not sure the syntax to update this value in the nested dictionary that is 'prefs'. It is nested because 'download' is also a dictionary.
Edit:
I am now using the AddAdditionalCapability() method which does not throw any errors when running the script now. However the download location is still not successfully changed via the commands below:
$options = New-Object OpenQA.Selenium.Edge.EdgeOptions
$options.AddAdditionalCapability('prefs', @{'download' = @{'default_directory' = "C:\temp"} } )
$driver = New-Object OpenQA.Selenium.Edge.EdgeDriver($options)
this solution in python is what I am trying to acomplish but in PowerShell. I am using Selenium version 3. I have thought about updating to version 4 but I am not sure that is available with EdgeDriver yet? I have found most EdgeDriver documentation to be slightly inconsistent with method and property names.
So, ultimately I am not sure where this code is going wrong, whether it is due to some syntax or other issue. For example this Microsoft EdgeDriver documentation says the 'AddAdditionalEdgeOption' method is how one would change the download location. But when I call
$options | Get-Member
I find that the method which I believe I need to use is called 'AddAdditionalCapability'. I am assuming this discrepancy is due to different selenium or edgedriver versions but I cannot confirm that anywhere.
Anywho, if anyone has ever used selenium and edgedriver to change the default download location using PowerShell (I am assuming atleast one person out there has done this haha) I would greatly appreciate some help!
答案1
得分: 1
以下是翻译好的内容:
<!-- language-all: sh -->
注意:
* **以下方法均不起作用** - 因此,此答案仅(在某种程度上)有助于您知道不应尝试什么。
* 对于愿意深入研究源代码的人,这是[`.AddAdditionalEdgeOption()`方法的定义](https://github.com/SeleniumHQ/selenium/blob/4d70bd80f40592c12170ca8382d54064278add6f/dotnet/src/webdriver/Edge/EdgeOptions.cs#L101),其中显示第二个参数的类型为 `object`。
---
如果[链接的帮助主题](https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/capabilities-edge-options#edgeoptions-object)中的“dictionary”指的是实现了`IDictionary`接口的.NET对象,那么您应该能够使用PowerShell [hashtable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Hash_Tables)字面量:
```powershell
$options.AddAdditionalEdgeOption(
'prefs',
@{ 'savefile.default_directory' = 'C:\temp' }
)
请注意,default_directory
属性嵌套在顶级的savefile
属性中,而不是download
(至少基于在"$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Preferences"
中找到的JSON)。
鉴于这种嵌套,问题是是否确实应该将此嵌套表示为表达属性路径的属性名称,如上所示,还是您实际上必须传递一个嵌套的字典(链接的帮助主题没有提供任何指导):
$options.AddAdditionalEdgeOption(
'prefs',
@{ savefile = @{ default_directory = 'C:\temp' } }
)
可以想象,字典必须作为JSON字符串传递,您可以通过ConvertTo-Json
从您的hashtable构建:
$options.AddAdditionalEdgeOption(
'prefs',
(@{ savefile = @{ default_directory = 'C:\temp' } } | ConvertTo-Json)
)
<details>
<summary>英文:</summary>
<!-- language-all: sh -->
Note:
* **None of the methods below work** - this answer is therefore only (somewhat) helpful in that you'll know what _not_ to try.
* For those willing to dig into the source code, here is the [definition of the `.AddAdditionalEdgeOption()` method](https://github.com/SeleniumHQ/selenium/blob/4d70bd80f40592c12170ca8382d54064278add6f/dotnet/src/webdriver/Edge/EdgeOptions.cs#L101), which shows that the second parameter is `object`-typed.
---
If "dictionary" in the [linked help topic](https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/capabilities-edge-options#edgeoptions-object) refers to a .NET object that implements the `IDictionary` interface, then you should be able to use a PowerShell [hashtable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Hash_Tables) literal:
$options.AddAdditionalEdgeOption(
'prefs',
@{ 'savefile.default_directory' = 'C:\temp' }
)
Note that the `default_directory` property is nested inside the top-level `savefile` property, not `download` (at least based on the JSON found in `"$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Preferences"`).
Given this _nesting_, the question is whether this nesting is indeed to be expressed as a property name expressing a property _path_, as above, or whether you must actually pass a _nested dictionary_ (the linked help topic provides no guidance):
$options.AddAdditionalEdgeOption(
'prefs',
@{ savefile = @{ default_directory = 'C:\temp' } }
)
Conceivably, the dictionary must be passed _as a JSON string_, which you can construct from your hashtable via [`ConvertTo-Json`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/ConvertTo-Json):
$options.AddAdditionalEdgeOption(
'prefs',
(@{ savefile = @{ default_directory = 'C:\temp' } } | ConvertTo-Json)
)
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论