0

I need help with the automated filling of a web page. I want to open the page in Edge, enter something in a field and then save what happens through a button and then close the tab again. Currently my code looks like this:

[system.Diagnostics.Process]::Start("msedge","https://mypage.com")
Sleep 3
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('MyTab') 
$wshell.SendKeys("%n{TAB}")
$wshell.SendKeys("%n{TAB}")
$wshell.SendKeys("%n{TAB}")
$wshell.SendKeys("%n{TAB}")
$wshell.SendKeys("%n{TAB}")
$wshell.SendKeys("%n{TAB}")
$wshell.SendKeys("{H}")
$wshell.SendKeys("{e}")
$wshell.SendKeys("{l}")
$wshell.SendKeys("{l}")
$wshell.SendKeys("{o}")
$wshell.SendKeys("%n{TAB}")
$wshell.SendKeys("%n{TAB}")
$wshell.SendKeys("%n{TAB}")
$wshell.SendKeys("{ENTER}")

As you can see I move on the page with tab and make all inputs by SendKeys, but here there is surely a better variant? The field where the entries are to be made, I have already figured out and is: <div class="DialogInput_Wrapper _DialogLayout Panel Focusable"><input type="text" name="personaName" class="DialogInput DialogInputPlaceholder DialogTextInputBase Focusable" tabindex="0" value="Goodbye"></div> respectively <input type="text" name="personaName" class="DialogInput DialogInputPlaceholder DialogTextInputBase Focusable" tabindex="0" value="Goodbye">

The button to save is: <button type="submit" class="DialogButton _DialogLayout Primary Focusable" tabindex="0">Save</button>

Can I even make entries this way or is the "tab" variant the seemingly best solution? Can it work at all if I make it a ComObject?

Please give some hints or help. Thank you

Kind regards Roman

I had tried ie-code, but then Explorer does not display the page correctly. The code used was:

$mytext="hello"
$url = "https://mypage.com"
$ie = New-Object -com InternetExplorer.Application
$ie.visible = $true
$ie.navigate($url)
while($ie.Busy) { Start-Sleep -Milliseconds 100 }
$ie.document.IHTMLDocument3_getElementByName("personaName").value = $mytext
$ie.document.IHTMLDocument3_getElementById("Save").click()

This would, I think, theoretically be the right code, but how can I pick out the individual components of the web page for a ComObject, similar to the code with: $ie.document.IHTMLDocument3_getElementByName("personaName").value = $mytext

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • PS is written in c# so any code in c# can be converted to PS. Best to look at a ASP.NET click event : https://stackoverflow.com/questions/44547143/c-sharp-code-behind-for-button-click-event-asp-net – jdweng May 01 '23 at 21:07
  • Thank you for the note, but then I need the webbrowser control? – Roman Milek May 01 '23 at 21:33
  • GUI scripting (simulating interactive user input) is inherently unreliable. I have no personal experience with it, but perhaps [Selenium](https://www.selenium.dev/) is a robust alternative; the third-party [Selenium PowerShell Module](https://github.com/adamdriscoll/selenium-powershell) is a PowerShell wrapper for it, but I don't know if it still works (the project is looking for maintainers). – mklement0 May 01 '23 at 21:45
  • @jdweng, not that it matters in this context, but to be clear: what specific language PowerShell is written in is irrelevant. What matters is that it (a) runs on .NET and (b) offers a scripting language that provides access to _most_ .NET APIs. – mklement0 May 01 '23 at 21:48

1 Answers1

0

You need a better understanding of how HTTP really works. HTTP has two pieces 1) The client 2) The server. The client can be implemented with a browser (contains a viewer) or a HTTP client (without a viewer). The client sends a HTTP request to the server and the returns a response. The request has a URL and optional headers and body. The response contains just headers and/or body.

There are 5 steps that normally occur that repeat multiple times

  1. Client sends a request
  2. Server receives request
  3. Server processes the request
  4. Server sends a response
  5. Client receives a response.

In your case your client first sends a request to a URL and get back back a webpage in the response to the client. Your case the response is HTML code. The client than parses the html to find the button click event. When the click event is activate a second request is than sent to the server with a different URL and can contain a route to a new html page in the server.

The parsing can be done using the library function GetElementBy. Do you need to see the webpage while your code is running or send request and get back a response? Send keys is not the best way of working with HTML. It is better to use a HTML library to parse the HTML webpage (often referred to a scraping). See following : How to parse the HTML of a website with PowerShell

jdweng
  • 33,250
  • 2
  • 15
  • 20