2

How can I read source of the open page in TWebBrowser in firemonkey (RadStudio XE7 - Firemonkey for Android)

In VCL I simply used WebBrowser1.document but in firemonkey it has an error

TWebBrowser does not contain a member named document

How can I fix that?

hrskrs
  • 4,447
  • 5
  • 38
  • 52
ARKhoshghalb
  • 85
  • 1
  • 13
  • 1
    http://stackoverflow.com/q/22156463/62576 Short answer: You can't. Use `TIdHTTP` to retrieve the HTML directly from the server. – Ken White Feb 26 '15 at 16:14
  • @KenWhite Well, That's to bad. If I get it that way, how can I post a HTML form? (I mean filling the inputs and submit it) – ARKhoshghalb Feb 26 '15 at 16:16
  • That's a totally separate question, which I'm quite certain has been asked here before - do a search on `[delphi][indy] web form` (including the brackets) and a few variations, and you'll find them. – Ken White Feb 26 '15 at 16:20
  • 1
    Depending on the `enctype` of the HTML `
    ` element, posting an HTML form with Indy is done by passing either a `TStrings` of `name=value` pairs (enctype=application/x-www-form-urlencoded) or a `TIdMultipartFormDataStream` (enctype=multipart/form-data) to the `TIdHTTP.Post()` method.
    – Remy Lebeau Feb 26 '15 at 18:35
  • @RemyLebeau Thank you very much but I didn't understand very well. Can you give me a sample code? – ARKhoshghalb Feb 26 '15 at 18:50
  • @SAKT: Like Ken said, search around. Examples have already been posted many times before. – Remy Lebeau Feb 26 '15 at 19:03
  • @KenWhite I would not recomend using TIdHTTP to retrive HTML unless there is a way that you can show that specific HTML inside the TWebBrowser. Why? Becouse you would actually be retrieveing HTML twice and there is no guarantee that the page hasn't change during the process. Infact if the web page uses a hidden field for monitoring web sesion the HTML retrieved with TIdHTTP will definitly be different than the one in TWebBrowser – SilverWarior Feb 26 '15 at 20:21
  • 1
    @SilverWarior: You can load the WebBrowser from memory or a local file. You can't retrieve the content from it, however; I'm suspecting that's the case because it's simply a wrapper around the iOS/Android equivalents, and they don't expose that capability. Therefore, if you want access to the HTML, you have to retrieve it a different way, whether it's via TIdHTTP, THttp (ICS), or another set of components. It doesn't matter whether it retrieves the page twice or not; if you need both TWebBrowser and the underlying HTML, you have no choice. – Ken White Feb 26 '15 at 20:30
  • @KenWhite It does matter if you retrieve web page once or twice. Becouse when you are retrieveing it twice the second time you retrieve it the page might have already been changed. Infact depending on sesion managing the web page that was retrieved firs might not be valid anymore so all posts from it will be denied by the server straight away. If you don't belive me make a quick web application using IntraWeb components ad use strict sesion managing (the one that doesen't alows you to use return button) and you will see what I'm talking about. – SilverWarior Feb 26 '15 at 20:55
  • @SilverWarior: I understand that completely. I'm saying that *if you're using the FMX TWebBrowser**, you cannot directly access the HTML, and if you **must both** use TWebBrowser and access the HTML, you have no choice but to execute the retrieval twice. If you can't do so, you can't use `TWebBrowser` as the visual control, because you *can't access the HTML directly from that container*. I'm not *recommending* anything; I'm stating a fact. *You can't get to the island except by boat. It's too far to swim, there's no room to land a plane, it's too overgrown to helicopter or parachute in.* – Ken White Feb 26 '15 at 21:04
  • continued: In the same vein: *You can't directly access the HTML from TWebBrowser. If you need access to the HTML you have to retrieve it another way. If you need both the visual representation in TWebBrowser and access to the HTML itself, you'll need to either replace TWebBrowser with another container that supports access to the HTML or you'll have to retrieve the HTML twice, once in TWebBrowser for display and once for access to the underlying HTML.* It's not a matter of believing you; it's the facts of the matter and the choices available. Don't believe me? Show me another option. – Ken White Feb 26 '15 at 21:06
  • Or, download the HTML once manually, and then load it into TWebBrowser using its [`LoadFromStrings()`](http://docwiki.embarcadero.com/Libraries/en/FMX.WebBrowser.TCustomWebBrowser.LoadFromStrings) method if you need to display it visually. – Remy Lebeau Nov 13 '15 at 02:00

1 Answers1

0

The sad answer is that you can't access the source code of a webpage directly from the TWebBrowser. You can't access it because the TWebBrowser in FireMonkey is cross-platform and that means it must work on all platforms. Android and iOS does not support and allow this, which means the Windows Desktop version of TWebBrowser can't allow this either.

You will have to use a different component to do this, probably a third party component.

I am not sure if this will help you or not, but here is a link to something that might help: http://firemonkeylessons.blogspot.tw/2015/01/get-htmljson-from-twebbrowser.html

Shaun Roselt
  • 1,650
  • 5
  • 18
  • 44