Error filling an input in powershell with ie?

1

It seems that the data of the page does not arrive to me, this one opens to me, but it says to me that the value is null.

I've tried with id and neither, I'm in version 5.1 of power shell.

This is the code:

  clear-host

  $ie = new-object -com "InternetExplorer.Application"


    $ie.navigate2("https://www.youtube.com/?hl=es&gl=ES")

    $ie.visible = $true

    $ie.Document.getElementsByTagName("input").value = "asdf"

And this is the error:

    
asked by ortiga 10.04.2018 в 20:53
source

1 answer

3

I have not been very clear about the requirement of your application, but from what I understood you are mixing 2 topics:

One is the ability to open a browser at a certain URL and the other can complete an input within a URL.

If you want to be able to open a browser with a search on YouTube, one way to do it is as follows:

$search = "powershell"
$search.Replace(" ","+")
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate2("https://www.youtube.com/results?search_query=$search")
$ie.visible = $true

Where the variable search specifies the value to search. If you look at the second line, I used the Replace method because when doing the search with terms that contain spaces, they are replaced with plus signs (+).

The result of the above must open a new IE window with the search on YouTube of the term "powershell".

Edited:

With your comment, I reviewed a bit and I could get to this:

$searchBar = "masthead-search-term"
$buttonId = "search-btn"
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate2("https://www.youtube.com/?hl=es&gl=ES")
$ie.visible = $true
while($ie.Busy) { Start-Sleep -Milliseconds 100 }
$doc = $ie.Document
$doc.getElementById($searchBar).value = "PowerShell"
$doc.getElementById($buttonId).click()

I added a wait while loading the page, and then filtered the search field and the search button. With the above you should be able to do what you have in mind.

    
answered by 11.04.2018 / 14:47
source