Powershell - How can I deal with this type of drop down in I

核能气质少年 提交于 2021-02-05 09:30:30

问题


Screenshot

How can I automate powershell to select the Company fro this type of dropdown because it's not a normal drop down but it shows values only once you start typing?

For e.g. in this case when I type C it shows Company. Refer the screenshot image above.

Following code inserts the value but it doesn't recognize it and gives an error asking to input value -

$test.document.getElementById("val1").value = "Company (Company)"

Below code which works for normal drop-downs doesn't work here -

$val = $test.getElementById("val1")
$val[1].selected = $True 
$val.FireEvent('onchange') > $null 

回答1:


An easier way to do this would be to skip automating an Internet Explorer window in its entirety, and instead simple capture the network operation and perform that in your scripts.

It's covered in detail in this blog post 'Faster Web Cmdlet Design using Chrome 65' (written by me) but to summarize:

  1. Open Chrome, or the new Edge, then hit F12 to open Developer Tools, and check the box 'Preserve Log' and then the filter 'All'
  2. Browse to your site in question
  3. Fill out the form
  4. Hit 'Post' or 'Submit' on your site and look for the matching action in the network activity list.
  5. Right-click and choose 'Copy As->Copy as PowerShell' and then use this as the basis for automating the form.

You'll end up with a PowerShell cmd in your clipboard that can be trimmed down to something like this, and is a great launching point for further automation.

Invoke-WebRequest -Uri "https://api.weather.com/v3/location/search?apiKey=6532d6454b8aa370768e63d6ba5a832e&language=en-US&query=90210&locationType=city%2Cairport%2CpostCode%2Cpws&format=json" `
    -Headers @{
    "method"="GET"
     "authority"="api.weather.com"
     "scheme"="https"
     "path"="/v3/location/search?apiKey=6532d6454b8aa370768e63d6ba5a832e&language=en-US&query=90210&locationType=city%2Cairport%2CpostCode%2Cpws&format=json"
      "accept"="application/json, text/plain, */*"
     "origin"="https://www.wunderground.com"  
  "accept-encoding"="gzip, deflate, br"
  "accept-language"="en-US,en;q=0.9"
  "dnt"="1"
}

This is part of how we test web applications at scale at the big bank where I work.

Why use this approach?

In this approach, you're testing the actual network interaction on the site, rather than filling out a IE Window and submitting it. It's generally dramatically easier to go this route, and will have a quicker time to viable output than dealing with the vagaries of JavaScript jQuery Validation and other front-end errors that can crop up.



来源:https://stackoverflow.com/questions/59864027/powershell-how-can-i-deal-with-this-type-of-drop-down-in-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!