Mark Garcia
Mark Garcia

Reputation: 221

How to fill HTML form fields and execute JavaScript?

My goal is to use an WPF app to add some field validation and logic to a legacy web form that my users have to interact with.

I've build the form fields and logic in WPF, and can open a browser page using cefsharp. Now I'm trying to figure out how to fill in the fields, and call some JavaScript functions.

So, for example I'm trying to open a frame by firing the below javascript after the page loads:

<a tabindex="0" href="javascript:;" onclick="showTicket();" onmouseover="window.status='Insert a new ticket';return true;" onmouseout="window.status=''">Ticket</a>

private void Ticket_Click(object sender, RoutedEventArgs e)
{
    BrowserControl.ExecuteScriptAsync("showTicket();");
}

But that doesn't actually render the frame, and I've tried calling the mainframe, but that didn't work either, do I need to find the html element and call the onclick button event instead, or maybe find the frame, but I think that should be handled by the script with I do not have access to.

The next thing that I need to do once the frame is open, is to fill in some html fields, like so:

<textarea class="form-control required" maxlength="255" name="Description"  tabindex="0" id="html001HTML" rows="3"></textarea>

The users will have to interact further with the form, so that it is why I am not going to handle everything programmatically. I just need to add a bunch of pre selected text to text fields.

Upvotes: 1

Views: 853

Answers (1)

Mark Garcia
Mark Garcia

Reputation: 221

After hours of searching, and working with Chrome's Devtools I finally found the answer: First I had to find the iframe, and then use contentWindow to call the function.

BrowserControl.ExecuteScriptAsync("document.getElementById('iframe1').contentWindow.Ticket()");

I found the answer here: Invoking JavaScript code in an iframe from the parent page

Upvotes: 1

Related Questions