【发布时间】:2023-04-09 04:54:02
【问题描述】:
在一些受人尊敬的成员的帮助下,我达到了可以在代码下方运行的地步。但碰巧这个脚本在中间用完了。 我得到的错误终于被粘贴了。
直到现在:我重新启动了我的系统多次,以确保它不是系统问题,但没有运气。
请注意,我在代码中添加了以下三行代码,再次确保它是由于某些 IE 造成的。
**# try few things
$result = $null
$ie.quit
get-process iexplore | stop-process**
代码从这里开始:
$controls = Get-Content ("d:\users\yarora\desktop\hkd1000.txt")
Add-Type -AssemblyName 'System.Web'
Function getStringMatch
{
Foreach ($control In $controls)
{
$ie = New-Object -COMObject InternetExplorer.Application
#for testing purpose
$control
#encode the special characters in URL like %,& etc.
$controlUri = [System.Web.HttpUtility]::UrlEncode($control)
$site = $ie.Navigate("https://www.xxxy.com/search/all?name=$controlUri")
#$ie.ReadyState
while ($ie.Busy){ sleep -Milliseconds 100 }
$link = $null
$link = $ie.Document.get_links() | where-object {if ($_.innerText){$_.innerText.contains($control)}}
$link.click()
while ($ie.Busy){ sleep -Milliseconds 100 }
[regex]$regex =
@'
(?s).+?<A href="/generics/.*?">(.*?)</A>
'@
$result = $regex.Matches($ie.Document.body.outerHTML) |
foreach {
[PSCustomObject]@{
Name = $control
Saltname = $_.Groups[1].value
}
}
$result | out-file d:\result.txt -append
# try few things
$result = $null
$ie.quit
get-process iexplore | stop-process
}
}
getStringMatch
错误
New-Object:检索具有 CLSID {0002DF01-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败 由于以下错误:800706bf 远程过程调用失败并且没有执行。 (HRESULT 的例外情况: 0x800706BF)。 在 D:\script.ps1:22 char:12 + $ie = 新对象 -COMObject InternetExplorer.Application + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ + CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
更新 根据 Micky 的建议,我将 -COMobject 置于 Foreach 循环之外。但是脚本在几个循环后仍然出错:
You cannot call a method on a null-valued expression.
At D:\desktop\hkdforsalt1000.ps1:41 char:9
+ $link.click()
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
【问题讨论】:
-
看起来你的资源已经用完了,试着把
$ie = New-Object -COMObject InternetExplorer.Application放在foreach循环之前。 -
这意味着
$link是$null。因此,要么存储在$control中的值有错误,要么该特定产品名称不再可用。您应该在调用它的 click() 方法之前检查 $link 的值并在此之后执行所有操作,因为它们都是基于链接被点击的假设。
标签: powershell