【问题标题】:PowerShell access properties of [object HTMLCollection][object HTMLCollection] 的 PowerShell 访问属性
【发布时间】:2016-08-23 21:03:12
【问题描述】:

我正在使用 PowerShell 将 IE 自动化为 com 对象。有一次,我选择了一个对象并将其存储在变量 $productImages 中:

$productImages = $ieInner.document.getElementById("product-image").getElementsByTagName("img")

在此之后,$productImages 是一个具有许多属性的对象。如果你只是在提示符下输入 $productImages,你会得到几十个,比如:

$productImages.length 为 1。在此脚本的先前版本(有效)中,我的选择器返回了许多对象,并将所有对象的 src 添加到数组中。此选择器仅返回一个,但 $productImages.src 是无效命令。我不明白如何获取产品的 src URL,这是我的最终目标。

【问题讨论】:

  • $productImages[0].src怎么样

标签: powershell internet-explorer com


【解决方案1】:

尝试以下操作查看$productImages的类型。在这种情况下,我相信它是一个包含一个元素的数组,因为您可以访问 length 属性。

$productImages.GetType()

访问数组的方法是使用以下格式:

$productImages[index].property

在您的情况下,必需的脚本应该是:

$productImages[0].src

如果您的变量的 GetType 有不同的值,请告诉我们。

【讨论】:

  • GetType() 确认 $productImages 是 "System.MarshalByRefObject" 的 ComObject 基类型 $productImages.length 如您所料显示为 1。但是 $productImages[0].src 什么也不返回如果我继续这些行,我可以将 src 放入另一个数组并最终访问它 $imgArray = @() foreach($img in $productimages) { $imgArray = $imgArray + $ img.src } $imgURL = $imgArray[0] $imgURL = $productImages |选择 * | ForEach-Object { $_.src } $savePath = $saveLocation+$productName+".jpg" Start-BitsTransfer $imgURL $savePath
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
相关资源
最近更新 更多