Select option from dropdown

2019-09-14 00:59发布

问题:

I'm working on some automation and I came across one dropdown where I'm having an issue to select an option.

Only way, I was able to get the element is trough IHTMLDocument3:

$dropdown = $ie.Document.IHTMLDocument3_getElementsByName("searchTypeChoice")

This is limiting my options how to select an option, since I can't use

.Options.Selected = $true

or anything else. I found these questions:

  • Powershell..select drop down menu from web page
  • Select option on dropdown list for web ui testing automation in windows powershell

This is the element I'm talking about:

Do you have any suggestions? Or maybe another method how to get the dropdown in other way?

回答1:

Enumerate the options, filter the one you want to select by its value (or inner text), then select it:

$dropdown.Options |
  Where-Object { $_.Value -eq 1 } |
  ForEach-Object { $_.Selected = $true }