自动化网站登录和表格填写? [英] Automate website log-in and form filling?

查看:14
本文介绍了自动化网站登录和表格填写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试登录网站并自动保存 HTML 页面(我希望能够定期执行此操作).从表面上看,这是一个典型的现代网站,如果用户直接导航到锁定"的 URL,则会弹出一个登录表单,登录后,用户将被重定向到预期的页面.

I'm trying to log in to a website and save an HTML page automatically (I want to be able to do this on a regular time interval). From the surface, this is a typical modern website where, if the user navigates directly to a "locked" URL, a log-in form pops up, and after logging in, the user is redirected to the intended page.

我尝试了机械化 (http://wwwsearch.sourceforge.net/mechanize/)但它没有找到登录所需的某些表单元素(隐藏元素具有一些值,这些值由用户单击登录"按钮时运行的 javascript 函数输入).

I gave mechanize a shot (http://wwwsearch.sourceforge.net/mechanize/) but it wasn't finding some form elements which were needed for login (hidden elements that have some values put in by a javascript function that runs when the user clicks the "log in" button).

我玩过 .NET 中的网络浏览器"控件,但很快就失去了兴趣,因为我什至无法让它在 Google 页面上提交查询.

I played a bit with the "web browser" control in .NET but quickly lost interest because I couldn't even get it to submit a query on the Google page.

我不在乎语言是什么;我会学习它来解决这个问题.至少它必须在 Windows 中工作.

I don't care what the language is; I'll learn it to solve this problem. At a minimum it has to work in Windows.

举个简单的例子,比如说,在 Google 搜索框中输入查询将是一个很大的好处.

A simple example, say, typing in a query into the Google search box would be a great bonus.

推荐答案

以我的经验,最可靠的方法是使用 javascript.它在 .Net 中运行良好.要进行测试,请在 Firefox 或 Internet Explorer 中依次浏览以下地址:

In my experience, the most reliable way is to use javascript. It works well in .Net. To test, browse to the following addresses one after another in Firefox or Internet Explorer:

http://www.google.com
javascript:function f(){document.forms[0]['q'].value='stackoverflow';}f();
javascript:document.forms[0].submit()

这会在 Google 上搜索stackoverflow".要使用 webbrowser 控件在 VB .Net 中执行此操作,请执行以下操作:

That performs a search for "stackoverflow" on Google. To do it in VB .Net using the webbrowser control, do this:

WebBrowser1.Navigate("http://www.google.com")
Do While WebBrowser1.IsBusy OrElse WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
    Threading.Thread.Sleep(1000)
    Application.DoEvents()
Loop
WebBrowser1.Navigate("javascript:function%20f(){document.forms[0]['q'].value='stackoverflow';}f();")
Threading.Thread.Sleep(2000) 'wait for javascript to run
WebBrowser1.Navigate("javascript:document.forms[0].submit()")
Threading.Thread.Sleep(2000) 'wait for javascript to run

注意 URL 中的空格是如何转换为 %20 的.我不确定这是否有必要,但它不会造成伤害.重要的是第一个 javascript 在一个函数中.对 Sleep() 的调用将等待 Google 加载以及 javascript 内容.如果页面加载失败,Do While 循环可能会永远运行,因此出于自动化目的,有一个计数器会在 60 秒后超时.

Notice how the space in the URL is converted to %20. I'm not certain if this is necessary but it can't hurt. It is important that the first javascript be in a function. The calls to Sleep() are to wait for Google to load and also for the javascript stuff. The Do While Loop might run forever if the page fails to load so for automation purposes have a counter that will timeout after, say, 60 seconds.

当然,对于 Google,您可以直接导航到 www.google.com?q=stackoverflow,但是如果您的站点有隐藏的输入字段等,那么这就是要走的路.仅适用于 HTML 网站 - Flash 是另一回事.

Of course, for Google you can just navigate directly to www.google.com?q=stackoverflow but if your site has hidden input fields, etc, then this is the way to go. Only works for HTML sites - flash is a whole other matter.

这篇关于自动化网站登录和表格填写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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