#powershell #internet-explorer #web-scraping
#powershell #internet-explorer #очистка веб-страниц
Вопрос:
Я хочу выбрать элемент списка (3M), указанный на прикрепленном изображении, с веб-сайта, использующего powershell. Я пробовал ниже, но безуспешно.Пожалуйста, предложите 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
Ответ №1:
Я думаю, вы были действительно близки к своему решению. У меня этот код сработал. Я вставил комментарии в строку, чтобы объяснить пару внесенных мной изменений.
$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.
Ответ №2:
Приведенный ниже код также может помочь нажать на опцию 3M в списке.
$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')
$link = $ie.Document.links | Where-Object {$_.outerText -eq '3M'}
$link.click()