我在MSDN中看到了很多关于如何在VS中使用MSHTML的例子.有谁知道我们是否以及如何使用MSHTML和VBA来打开网页?
谢谢.
接口名称末尾的数字是什么意思?我看到IHTMLDocument3-7没有成员(参见#5的示例),并且8有与手势相关的成员.该数字是从Windows版本派生的吗?
我正在使用带有WebBrowser控件的MSHTML,因为它让我可以访问WebBrowser没有的东西,例如文本节点.我在这里和网上看过几篇帖子,人们说你必须调用ReleaseComObject你引用的每个COM对象.所以,说我这样做:
var doc = myBrowser.Document.DomDocument as IHTMLDocument2;
我需要发布doc吗?如何body在此代码中:
var body = (myBrowser.Document.DomDocument as IHTMLDocument2).body;
RCW包装的这些对象是否会在没有更多引用的情况下立即释放它们?如果没有,最好使用终结器(而不是使用Dispose)为每个人创建一个包装器,一旦垃圾收集器启动就会释放它们(这样我就不用担心了处理他们)?
问题是,我的应用程序有内存泄漏,我相信这与此有关.根据ANTS内存分析器,其中一个函数(在许多其他恰好使用MSHTML对象的函数中)持有对Microsoft.CSharp.RuntimeBinder.Semantics.LocalVariableSymbol第2代中使用内存的对象顶部对象的一组对象的引用,这个是:
internal static string GetAttribute(this IHTMLDOMNode element, string name)
{
var attribute = element.IsHTMLElement() ? ((IHTMLElement)element).getAttribute(name) : null;
if (attribute != null) return attribute.ToString();
return "";
}
Run Code Online (Sandbox Code Playgroud)
不知道这里有什么问题,因为attribute它只是一个字符串.
这是ANTS分析器的实例保留图上显示的另一个函数(我添加了一堆FinalReleaseComObjects但仍然显示):
private void InjectFunction(IHTMLDocument2 document)
{
if (null == Document) throw new Exception("Cannot access current document's HTML or document is not an HTML.");
try
{ …Run Code Online (Sandbox Code Playgroud) 我有这样的代码:
Dim Document As New mshtml.HTMLDocument
Dim iDoc As mshtml.IHTMLDocument2 = CType(Document, mshtml.IHTMLDocument2)
iDoc.write(html)
iDoc.close()
Run Code Online (Sandbox Code Playgroud)
但是,当我加载这样的HTML时,它会执行其中的所有Javascripts以及从"html"代码请求某些资源.
我想禁用javascript和所有其他弹出窗口(例如证书错误).
我的目标是使用mshtml文档中的DOM以可靠的方式从HTML中提取一些标签(而不是一堆正则表达式).
或者是否有另一个IE/Office DLL,我可以加载HTML而不考虑IE相关的弹出窗口或活动脚本?
我在编辑模式下使用MSHTML控件.当我将文本中的内容复制并粘贴到我的控件中时,MSHTML控件会剥离标准HTML并保留那些不太受支持的VML标记.
如果我取消注册VML Dll(regsvr32 -u"%ProgramFiles%\ Common Files\Microsoft Shared\VGX\_vgx.dll),那么控件将按照我想要的方式运行并丢弃VML并保留HTML.
我没有能够以编程方式告诉MSHTML我不想要VML而是HTML.有任何想法吗?
我有这两种方法来获取当前的旅行日志条目并转到通过调用GetTravelLogEntry方法检索的日志条目:
public static ITravelLogEntry GetTravelLogEntry(WebBrowser webBrowser)
{
int HRESULT_OK = 0;
SHDocVw.IWebBrowser2 axWebBrowser = (SHDocVw.IWebBrowser2)webBrowser.ActiveXInstance;
IServiceProvider psp = axWebBrowser as IServiceProvider;
if (psp == null) throw new Exception("Could not get IServiceProvider.");
IntPtr oret = IntPtr.Zero;
int hr = psp.QueryService(ref SID_STravelLogCursor, ref IID_ITravelLogStg, out oret);
if ((oret == IntPtr.Zero) || (hr != HRESULT_OK)) throw new Exception("Failed to query service.");
ITravelLogStg tlstg = Marshal.GetObjectForIUnknown(oret) as ITravelLogStg;
if (null == tlstg) throw new Exception("Failed to get ITravelLogStg");
ITravelLogEntry ptle = null; …Run Code Online (Sandbox Code Playgroud) 我需要在服务器端呈现HTML页面并"提取"canvas元素的原始字节,以便将其保存到PNG.问题是,canvas元素是从javascript创建的(我使用jquery的Flot生成一个图表,基本上).所以我想我需要一种在没有实际使用浏览器的情况下从浏览器"托管"DOM + Javascript功能的方法.我决定使用mshtml(但对任何和所有建议开放),因为它似乎应该能够完全实现.这是一个ASP.NET MVC项目.
我搜索得很远,没有看到任何结论.
所以我有这个简单的HTML - 示例尽可能简单地说明问题 -
<!DOCTYPE html>
<html>
<head>
<title>Wow</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js" type="text/javascript"></script>
</head>
<body>
<div id="hello">
</div>
<script type="text/javascript">
function simple()
{
$("#hello").append("<p>Hello</p>");
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
从浏览器运行时产生预期的输出.
我希望能够将原始HTML加载到内存中,执行javascript函数,然后操作最终的DOM树.我不能使用任何类似System.Windows.WebBrowser的类,因为我的代码需要在服务环境中运行.
所以这是我的代码:
IHTMLDocument2 domRoot = (IHTMLDocument2)new HTMLDocument();
using (WebClient wc = new WebClient())
{
using (var stream = new StreamReader(wc.OpenRead((string)url)))
{
string html = stream.ReadToEnd();
domRoot.write(html);
domRoot.close();
}
}
while (domRoot.readyState != "complete")
Thread.Sleep(SleepTime);
string beforeScript = domRoot.body.outerHTML;
IHTMLWindow2 parentWin = domRoot.parentWindow;
parentWin.execScript("simple");
while (domRoot.readyState != "complete") …Run Code Online (Sandbox Code Playgroud) Windows窗体应用程序 - 操作输入元素 WinForm WebBrowser
虽然我熟悉HttpWebResponse/ HttpWebRequest登录网站,但我现在正在尝试使用mshtml库并发现一些奇怪的行为,我想看看是否有其他人可以帮助我在这里..
我有一个HTML登录页面,其中包含一个带有Username字段,Password字段和a 的java后端Button.
逻辑是非常基本的,我有一个内置的webbrowser构建了一个winform应用程序.在Document_Completed活动中,我使用以下代码输入我的设置并单击按钮.
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser.Url.ToString() == @"MyWebPage/signin")
{
HTMLDocument hdc = new HTMLDocumentClass();
hdc = (HTMLDocument)webBrowser.Document.DomDocument;
IHTMLElement elb = hdc.getElementById("login_button");
IHTMLInputElement elu = (IHTMLInputElement)hdc.getElementById("username");
IHTMLInputElement elp = (IHTMLInputElement)hdc.getElementById("password");
try
{
elu.value = "MyID";
elp.value = "MyPwd";
elb.click();
}
catch { }
}
}
Run Code Online (Sandbox Code Playgroud)
除了这个代码非常快,没有错误处理,它应该做的伎俩,它确实,部分..
有两种情况:
我启动该工具,它加载网页.
我手动点击按钮,我登录,点击退出,我回到登录页面
是否有人能够解释我为什么会发生这种情况以及如何通过当前设置(因此不使用HttpWebRequest)来解决这个问题.我没有看到在启动时加载页面或在注销后重定向之间的区别,但显然存在差异或我做错了什么.
对此事的任何反馈都非常感谢. …
我正在尝试通过PowerShell进行一些网络抓取,因为我最近发现它可以毫不费力地这样做.
一个很好的起点是只获取HTML,使用Get-Member,看看我能从那里做些什么,如下:
$html = Invoke-WebRequest "https://www.google.com"
$html.ParsedHtml | Get-Member
Run Code Online (Sandbox Code Playgroud)
我可用于获取特定元素的方法如下所示:
getElementById()
getElementsByName()
getElementsByTagName()
Run Code Online (Sandbox Code Playgroud)
例如,我可以在文档中获取第一个IMG标记,如下所示:
$html.ParsedHtml.getElementsByTagName("img")[0]
Run Code Online (Sandbox Code Playgroud)
然而,在对我是否可以使用CSS Selectors或XPath进行更多研究之后,我发现有未列出的方法可用,因为我们只是使用此处记录的HTML Document对象:
querySelector()
querySelectorAll()
Run Code Online (Sandbox Code Playgroud)
所以不要这样做:
$html.ParsedHtml.getElementsByTagName("img")[0]
Run Code Online (Sandbox Code Playgroud)
我可以:
$html.ParsedHtml.querySelector("img")
Run Code Online (Sandbox Code Playgroud)
所以我期待能够做到:
$html.ParsedHtml.querySelectorAll("img")
Run Code Online (Sandbox Code Playgroud)
...为了获得所有的IMG元素.我发现的所有文档和我已经完成的谷歌搜索都支持这一点.但是,在我的所有测试中,此函数都会使调用进程崩溃,并在事件日志(0xc0000374)中报告堆损坏异常代码.
我在Windows 10 x64上使用PowerShell 5.我在Win10 x64虚拟机中尝试过它,这是一个干净的构建,只是修补了.我也尝试过在Win7 x64上升级到PowerShell 5.我在PowerShell 5之前没有尝试过任何东西,因为我们这里的所有系统都已经升级了,但是我可能会有时间假设一个新的vanilla虚拟机进行测试.
有没有人以前遇到过这个问题?到目前为止,我所有的研究都是死路一条.querySelectorAll有替代品吗?我需要抓取在不可预测的布局中有可预测的标签集的页面,并且可能没有分配给标签的ID或类,所以我希望能够使用允许结构/嵌套/通配符的选择器.
PS我也试过在PowerShell中使用InternetExplorer.Application COM对象,结果是一样的,除了PowerShell崩溃Internet Explorer崩溃.这实际上是我原来的方法,这里是代码:
# create browser object
$ie = New-Object -ComObject InternetExplorer.Application
# make browser visible for debugging, otherwise this isn't necessary for function
$ie.Visible = $true
# browse to page
$ie.Navigate("https://www.google.com")
# wait till browser is not busy
Do { Start-Sleep …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过Delphi 10 Seattle中的MSHTML解析器解析HTML.它运行正常,但是ARTICLE标签混淆了它,解析了ARTICLE元素没有innerHTML和children,尽管它们在那里.
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Variants,
ActiveX,
MSHTML;
procedure DoParse;
var
idoc: IHTMLDocument2;
iCollection: IHTMLElementCollection;
iElement: IHTMLElement;
V: OleVariant;
HTML: String;
i: Integer;
begin
Html :=
'<html>'#10+
'<head>'#10+
' <title>Articles</title>'#10+
'</head>'#10+
'<body>'#10+
' <article>'#10+
' <p>This is my Article</p>'#10+
' </article>'#10+
'</body>'#10+
'</html>';
v := VarArrayCreate( [0,1], varVariant);
v[0]:= Html;
idoc := CoHTMLDocument.Create as IHTMLDocument2;
idoc.designMode := 'on';
idoc.write(PSafeArray(System.TVarData(v).VArray));
idoc.close;
iCollection := idoc.all as IHTMLElementCollection;
for i := 0 to iCollection.length-1 do
begin
iElement …Run Code Online (Sandbox Code Playgroud)