Accessing html elements in web viewer

When I load a website via web viewer/browser, how can I access the individual html elements, like forms, buttons etc. to fill forms and click buttons etc. via code?

Desktop, Mobile or Web project?

Mac? Win? Linux? iOS?

Mac, Linux and Windows…

take a look at executejavascript

Desktop, Mobile or Web project?

Only desktop. I do that all the time with Delphi, but xojo seems a little tricky to do that (submitting forms and filling form fields, via browser, not via http component and POST).

How do you do it in Delphi ?

For instance, filling up an input field ?

In MBS Xojo Plugins we have a lot of HTMLViewer extensions which may help.

RunJavaScript in IEWindowMBS class for Windows or IERunJavaScriptMBS in older plugins.
Or newer Evaluate method in IEDocumentMBS class.

If you use WebKit on Windows, you can use ExecuteJavaScript in ChromiumBrowserMBS or ChromiumFrameMBS classes.

HTMLViewer.EvaluateJavaScriptMBS for MacOS or EvaluateJavaScript method in WebViewMBS class.

For MacOS you can use WKWebViewControlMBS control instead to get WebKit 2.x and run JavaScript there, too.

For Linux use EvaluateScript in LinuxWebViewMBS class or EvaluateScript in LinuxJavaScriptContextMBS class.

Without a HTMLViewer you can run JavaScript with JSContextMBS (MacOS) or JavaScriptEngineMBS (Cross platform)

[quote=470302:@Michel Bujardet]How do you do it in Delphi ?

For instance, filling up an input field ?[/quote]

I use a webbrowser, its document interface as ihtmldocument2, then IHTMLFormElement in connection with IHTMLInputElement, IHTMLSelectElement, IHTMLTextAreaElement and so on to set values of the fields.

In Xojo, you will use ExecuteJavaScript, and address elements for instance like so:

Dim js as string js = js + "document.getElementByName('UserName').value='John Smith'; HTMLViewer1.executejavascript(js)

This fills up the UserName input field with “John Smith”.

[quote=470359:@Michel Bujardet]In Xojo, you will use ExecuteJavaScript, and address elements for instance like so:

Dim js as string js = js + "document.getElementByName('UserName').value='John Smith'; HTMLViewer1.executejavascript(js)

This fills up the UserName input field with “John Smith”.[/quote]

How do I get the “document” from the html viewer in Xojo so I can actually use the document.getElementByName?

document is a global in JavaScript.
No need to get it.

[quote=470362:@Christian Schmitz]document is a global in JavaScript.
No need to get it.[/quote]

I dont get it. The sample code above wont run for me. I dont know how that works in Xojo. What else beside dropping the htmlviewer on the form I will have to do in order to be able to execute the above sample code? Something else I need to drop on the form?

executejavascript is for Web projects only!

ExecuteJavaScript is a method of HTMLViewer. In my example, I fill an input tag with ID UserName. In your particular use, you must look at the input names with a browser developer tools.

You can put the sample code in a button’s event, for instance.

See http://documentation.xojo.com/api/deprecated/htmlviewer.html#htmlviewer-executejavascript

Christian, ExecuteJavaScript is also a method of DESKTOP HTMLViewer :smiley:

See the link above to the LR.

You still need a plugin if you need a result :slight_smile:

You will need to be fluent in Javascript or Google-fu. The code Michel provided is just illustrative - it’s got a syntax error and wouldn’t even compile. This is not something you’re going to be able to figure out by copy and pasting bits that look right.

[quote=470359:@Michel Bujardet]In Xojo, you will use ExecuteJavaScript, and address elements for instance like so:

Dim js as string js = js + "document.getElementByName('UserName').value='John Smith';
[/quote]

This is missing the closing double-quote ("). Put it after the semi-colon (;).

No you just need the HTMLviewer.

The general method is this: you construct your js string in Xojo and send it off to the HTMLViewer to be executed. If you want to get data back, your js will need to set window.status or document.title. These will cause the Xojo HTMLViewer events TitleChanged or StatusChanged to fire.

If the javascript you need is going to be lengthy, best way is to make javascript methods that you include in the HTML document that you load into the HTMLViewer with the LoadPage method. You still use ExecuteJavaScript to get these methods called, but the js you ask to run that way can then be short.

Of course, I am assuming you have control of the source of the HTML that you load. If you don’t, then it’s unclear to me that what you ask is possible at all.

I believe it is quite possible to do what Guenter wants to do. At least least the filling up of forms, even on a web site page, as long as he knows the name of fields.

As for clicking a button, the JS Click() method should take care of it.

https://www.w3schools.com/jsref/met_html_click.asp

So that wont help me at all.

I have a Win Desktop app, with a htmlviewer, and just want to fill the formfields and submit the form.
In Delphi, I use the document interface, exposed by the browser control, but I cant find something similar in the htmlviewer control.