2

I'm load a webpage in a TWebbrowser but the webpage isn't displaying correctly and I am unable to change the coding.

How can I alter an inline style in Delphi after the page has loaded?

this is the code i'm trying to change:

<td width="200px" valign=top style="background-color:#576299; height:800px;">

to this:

<td width="200px" valign=top style="display:none; background-color:#576299; height:800px;">

I first I thought I could just inject a css style sheet using this method: CSS and TWebbrowser delphi but there is no class to override, i could setup a style for all tables but there are other tables on the page that i want to display.

Is there anyway to search for the html i'm looking for and replace it with something else?

Community
  • 1
  • 1
user1398287
  • 5,245
  • 5
  • 21
  • 25

1 Answers1

1

If a webpage is not rendered expectedly, it means the webbrowser control does not use the same rendering policy as MSIE uses. To fix that, you might need to use FEATURE_BROWSER_EMULATION (see this post)

If you want to change DOM, there are several ways to archive that:

Way 1: Use the magic oleobject. When the page is loaded successfully, you can call WebBrowser1.oleobject.getElementById('foo').style := 'new_style';.

Way 2: Use the interfaces in MSHTML_TLB.pas. Demo code as follows.

var
  D2Ptr: IHTMLDocument2;
  ElemPtr: IHTMLElement;
begin
  if Supports(WebBrowser1.Document, IHTMLDocument2, D2Ptr) then
  begin
    ElemPtr := D2Ptr.getElementById('foo'); // If the td has an id, you can use this method.
    if ElemPtr <> nil then
      try
        ElemPtr.style := ElemPtr.style + '; display:none';
      except
      end;
  end;
end;

Note that, if the element you are looking for, does not have an id. You might first find its parent, who has an id, and then travel through its child elements.

Community
  • 1
  • 1
stanleyxu2005
  • 8,081
  • 14
  • 59
  • 94