2009-09-18 62 views
0

我如何检索文本从德尔福的网站文本框例如假设我在谷歌搜索框中键入 ''虎''如何从该搜索框中检索文本将wm_gettext或getwindowtext工作? 我正在使用delphi 7如何检索网站文本框中的文本

+0

您是否试图从Internet Explorer的正在运行的实例中获取文本?或者一个tWebBrowser组件? – skamradt 2009-09-18 15:51:25

+0

我想从一个正在运行的Internet Explorer/firefox /其他某个浏览器的实例中获取文本 – 2009-09-19 05:19:23

回答

3

试试这段代码。

只适用于Internet Explorer。在Windows Vista中,IE8ÿ德尔福测试2007

Uses 
SHDocVw, 
mshtml; 

procedure GetTextFromEditIExplorer(ListStr: TStringList); 
var 
    ShellWindow   : IShellWindows; 
    Web_Browser   : IWebbrowser2; 
    reg_Shell_window  : IDispatch; 
    Dummy     : IHTMLDocument2; 
    ovElements    : OleVariant; 
    Document    : Variant; 
    WindowsCount   : Integer; 
    ElementsCount   : Integer; 
    FormsCount    : Integer; 
begin 
    ShellWindow := CoShellWindows.Create; //Provides access to the collection of open Shell windows 
    for WindowsCount := 0 to ShellWindow.Count do  //iterate through number of windows in the Shell windows collection 
    begin 
    reg_Shell_window := ShellWindow.Item(WindowsCount);  //Returns the registered Shell window for a specified index. 
    if reg_Shell_window = nil then Continue; //go to next reg window 
    reg_Shell_window.QueryInterface(iWebBrowser2, Web_Browser); // determines if an interface can be used with an object 
    if Web_Browser <> nil then 
    begin 
     Web_Browser.Document.QueryInterface(IHTMLDocument2, Dummy); 
     if Dummy <> nil then 
     begin 
     Web_Browser  := ShellWindow.Item(WindowsCount) as IWebbrowser2; 
     Document  := Web_Browser; 
      for FormsCount := 0 to Document.forms.Length - 1 do 
      begin 
      ovElements := Document.forms.Item(FormsCount).elements; 
      for ElementsCount := 0 to ovElements.Length - 1 do 
      begin 
       try 
       if (CompareText(ovElements.item(ElementsCount).tagName, 'INPUT') = 0) and (CompareText(ovElements.item(ElementsCount).type, 'text') = 0) then 
       ListStr.Add('Control Name ['+ovElements.item(ElementsCount).Name+']'+' Type -> '+ovElements.item(ElementsCount).Type+' -> Value ['+ovElements.item(ElementsCount).Value+']'); 
       except 
       ListStr.Add('Error Reading element n° '+IntToStr(ElementsCount)); 
       end; 
      end; 
      end; 
     end; 
    end; 
    end; 
end; 

procedure TForm1.btn1Click(Sender: TObject); 
var 
List : TStringList; 
begin 
    List:=TStringList.Create; 
    GetTextFromEditIExplorer(List); 
    ShowMessage(List.Text); 
end; 

编辑:

奥马尔遗憾的是没有简单的解决您的问题。这是因为每个浏览器都使用不同的界面来与信息进行交互。

这里有一些建议

  • Firefox使用XPCOM,你可以研究一下。
  • 尝试使用DDE(动态数据交换)。
  • 您可以使用WatiN,它是一个与firefox和iexplorer一起工作的.net库。你可以看到this article以了解如何与delphi win32中的.net程序集进行交互。

再见。

+0

感谢rruz :)这段代码似乎很难!是他们更简单的方法吗?我不是说我不能够使用这段代码,但生病了,必须学习一些新的东西,如ole和东西,其次,我想赶上从Firefox的编辑框。 – 2009-09-19 07:21:32

+0

+1好回答RRUZ。我同意没有简单的方法来实现这一点,特别是如果要支持多个浏览器。很遗憾,这种事情没有共同的钩子。 – Argalatyr 2009-09-20 17:03:48

+0

谢谢,我已经使用dde从浏览器中成功地获取网址,但我发现如何使用dde从网页的文本框中检索文本的任何帮助。你可以指出使用dde来完成这个任何例子 – 2009-09-21 08:32:03

0

最简单的方法是使用页面原始HTML代码的正则表达式。

+0

但是您可以在公共浏览器上运行实例吗?请帮忙! – 2009-09-20 04:12:51