WatiN - 等待 Cookie

发布于 2024-10-19 13:58:01 字数 989 浏览 7 评论 0原文

我被指派去创建一种“某种”网络爬虫。任务是访问某些网站,输入一条数据,然后提交该数据以接收结果。

我发现 WatiN 是获取这些数据的一个很好的工具,因为它是开源的,并且具有我需要的所有功能 - 即使这是对测试 API 的滥用(这是滥用吗?)。

无论如何 - 真正的问题是,

Browser.WaitForComplete();

不会等待所有内容加载。看起来它只是等待最后一个 body 标签然后返回。然而,我相信当页面加载完成时,cookie 不会生成,因为它是由 ASP.NET 后端生成的 - 它似乎是在看起来像 AJAX 或类似内容的交互式对象之后加载的。

我不拥有或托管该网站,因此只能根据我从浏览器和我可以使用的其他工具中看到的信息来收集信息。

有什么办法让WatiN等待cookie更新吗?

提前致谢。

约翰.

编辑:

作为我目前正在做的解决问题的示例(这有点不正统,但它正在立即解决)。

Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();

var cookie = Browser.Eval("document.cookie");

现在你可能明白为什么我想找到一个真正的解决方案了。

谢谢

I have been assigned to create a 'kind of' web creeper. The tasks is to go on certain websites, enter a piece of data then submit that data to receive a result.

I have found WatiN to be a great tool at getting this data as it's open source and has all the functionality I need - even if this is misuse of a Testing API (Is it misuse?).

Anyway - What the real question is, is that the

Browser.WaitForComplete();

Does not wait for EVERYTHING to load. It seems it just waits for the last body tag then returns. However I believe the cookie is not generated when the page is finished loading as it's being generated by an ASP.NET back end - it seems to load after an interactive object which looks like AJAX or something along them lines.

I do not own or host the web site therefore can only gather information based on what I see from the browser and other tools at my disposal.

Is there any way to get WatiN to wait for the cookie to be updated?

Thanks in advanced.

John.

Edit:

As an example of what I'm doing to fix the issue at the minute (Which is a bit unorthodox but it's fixing it for the minute).

Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();
Browser.WaitForComplete();

var cookie = Browser.Eval("document.cookie");

Now you may understand why I want to find a real solution.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-10-26 13:58:01

我不确定是否可以捕获此事件,因为我怀疑页面上一定有 JavaScript 正在运行,最终会更新 cookie。

如果你能区分“旧”和“更新”cookie,你可以尝试这样的方法:

// Maximum number of loops, to avoid infinite loops
int maxLoops = 10;

// Wait for the page to complete
Browser.WaitForComplete();

// Grab a copy of the current cookie
var cookie = Browser.Eval("document.cookie");

// HACK: Dirty hack to wait for cookie to be updated
while (CookieIsNotUpdated(cookie) && i++ <= maxLoops)
{
    Browser.WaitForComplete(); // (Or other wait/sleep)
    cookie = Browser.Eval("document.cookie");
}

这不是一个很好的方法,但它应该在没有更好的方法的情况下工作。

I'm not sure could capture an event for this, as I suspect there must be javascript running on the page that ultimately updates the cookie.

If you can tell the difference between an "old" and "updated" cookie, you could try something like this:

// Maximum number of loops, to avoid infinite loops
int maxLoops = 10;

// Wait for the page to complete
Browser.WaitForComplete();

// Grab a copy of the current cookie
var cookie = Browser.Eval("document.cookie");

// HACK: Dirty hack to wait for cookie to be updated
while (CookieIsNotUpdated(cookie) && i++ <= maxLoops)
{
    Browser.WaitForComplete(); // (Or other wait/sleep)
    cookie = Browser.Eval("document.cookie");
}

This isn't a nice way of doing it, but it should work in the absence of anything better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文