首页 > 解决方案 > 使用 Powershell 在网站中选择列表项

问题描述

我正在寻找使用 powershell 从网站中选择附件图像中给出的列表项 (3M)。我在下面尝试过,但没有运气。请建议 HTML 代码

$ie = New-Object -ComObject InternetExplorer.Application
$ie.visible=$true
$ie.navigate2('https://www.nseindia.com/companies-listing/corporate-filings-insider-trading')
while($ie.Busy) {Start-Sleep 1}
$dropdown = $ie.Document.getElementsByClassName('dayslisting')
($dropdown | where {$_.innerHTML -like "3M"}).Selected = $true

标签: powershellinternet-explorerweb-scraping

解决方案


我认为您非常接近您的解决方案。这段代码对我有用。我在内联注释以解释我所做的几个更改。

$ie = New-Object -ComObject InternetExplorer.Application
$ie.visible=$true
$ie.navigate2('https://www.nseindia.com/companies-listing/corporate-filings-insider-trading')
while($ie.Busy) {Start-Sleep 1}
$dropdown = $ie.Document.body.getElementsByClassName('dayslisting') # You needed to select .body here to use getElementsByClassName
$3m = ($dropdown[0].all | where {$_.innerHTML -like "3M"}) # Dropdown is a list because classes can be assigned to more than one element. I'm getting all elements of the first list, but you could do something better if you wanted.
$3m.click() # Click the "3M" button.

推荐阅读