c#WebBrowser-如何在文档加载完成后等待javascript完成运行? [英] c# WebBrowser- How can I wait for javascript to finish running that runs when the document has finished loading?

查看:608
本文介绍了c#WebBrowser-如何在文档加载完成后等待javascript完成运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个项目,涉及从供应商的网站上抓取一些产品数据(有他们的祝福,但不是他们的帮助)。我在C#商店工作,所以我使用.NET Windows Forms WebBrowser控件。

I'm working on a project that involves scraping some product data off of a vendor's web site (with their blessing, but not their help). I'm working in a C# shop, so I'm using the .NET Windows Forms WebBrowser control.

我正在响应文档已完成事件,但我发现我必须稍微调试一下,否则数据不会显示出来我期望它在DOM中的位置。

I'm responding to the document completed event, but I'm finding that I have to thread sleep for a little bit, or else the data doesn't show up where I expect it to in the DOM.

在查看页面上的javascript时,我可以看到它在页面加载完成后动态改变现有的DOM内容(设置someDomElement.innerHTML)。它没有进行任何ajax调用,它使用的是原始页面加载中已有的数据。 (我可以尝试解析该数据,但它嵌入在javascript中并且有点混淆。)显然,我以某种方式获取文档已完成事件在javascript运行完毕之前。

In looking at the javascript on the page, I can see that it is dynamically altering the existing DOM content (setting someDomElement.innerHTML) after the page finishes loading. It's not making any ajax calls, it's using data it already has from the original page load. (I could try and parse for that data, but it is embedded in javascript and it's a bit obfuscated.) So evidently I'm somehow getting the document completed event Before the javascript has finished running.

最终可能会有很多页面要刮掉,所以等待半秒或者其他什么东西都不太理想。我想只等到所有在文档就绪/页面加载时启动的JavaScript在我检查页面之前完成运行。有没有人知道这样做的方法?

There could eventually be a lot of pages to scrape, so waiting around for a half second or whatever is really far less than ideal. I would like to only wait until all the JavaScript that starts on document ready / page load has finished running before I examine the page. Does anyone know of a way to do that?

我想文件完成事件不应该在那之前开火,对吗?但它肯定是。也许某个页面javascript正在使用setTimeout。有没有办法判断是否有待处理的超时?

I suppose the document completed event shouldn't fire until then, right? But it definitely appears to be. Maybe somewhere the page javascript is using a setTimeout. Is there a way to tell if there any timeouts pending?

感谢您的帮助!

推荐答案

你可以


  1. 假设数据的解析永远不会改变,看看Javascript如何处理数据并做在页面加载时立即检索数据同样如此

  2. 将javascript注入网页并检测DOM修改以了解何时从C#中获取数据

  3. 使用PhantomJS编写纯粹的JavaScript解决方案

这篇关于c#WebBrowser-如何在文档加载完成后等待javascript完成运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆