如何以编程方式按下网站的登录按钮 [英] How to press a login button of a website programmatically

查看:42
本文介绍了如何以编程方式按下网站的登录按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将网站加载到 winform 中的 WebBrowser.我希望使用日志登录并通过 vb.net 自动通过

我在页面加载后执行以下代码

 WebBrowser1.Document.GetElementById("e").SetAttribute("value", "email@address")WebBrowser1.Document.GetElementById("p").SetAttribute("value", "password")WebBrowser1.Document.Forms(0).InvokeMember("submit")

我可以填写需要包含日志的框并通过,但我无法自动触发网页的登录按钮.经过研究,我确信我正确地复制了我找到​​的样本,但显然存在问题.没有按下按钮",因为我什么也没发生.当我手动按下网站登录时,它会使用我填写的数据正确登录.

网页链接是https://www.weatherzone.com.au/customise.jsp

相关源数据如下

 
<div style="margin-bottom: 1em;"><label for="e">输入您的电子邮件地址:</label><输入样式="宽度:95%;"type="text" name="e" id="e"/><br/>

<div style="margin-bottom: 1em;"><label for="p">输入您的密码:</label><输入样式="宽度:95%;"type="password" name="p" id="p"/><br/>

<div style="margin-bottom: 1em;"><input type="checkbox" name="rmbr" id="rmbr" value="1"/><label for="rmbr" style="display: inline;">记住我(如果您是这台计算机的普通用户,请选中此框)</label>

<div style="text-align: right; margin-top: 20px; padding-top: 10px; border-top: 1px solid silver;"><input class="submit btn" type="submit" name="target" value="login"/>

<input type="hidden" name="dest" value="/"/></表单>

解决方案

我应用了这个逻辑:

而且它工作正常.它重定向到相同的 url 并显示一条红色的未经身份验证的消息.

代码:

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) 处理 WebBrowser1.DocumentCompleted如果 (e.Url.ToString().StartsWith("https://www.weatherzone.com.au")) 然后WebBrowser1.Document.GetElementById("e").SetAttribute("value", "email@address.com")WebBrowser1.Document.GetElementById("p").SetAttribute("value", "password")WebBrowser1.Document.All().GetElementsByName("target").Cast(Of HtmlElement).First().InvokeMember("click")万一结束子Private Sub Form1_Load(sender As Object, e As EventArgs) 处理 MyBase.LoadMe.WebBrowser1.Navigate("https://www.weatherzone.com.au/customise.jsp")结束子

为了使代码更稳定,添加必要的空检查.

I am loading a website into a WebBrowser in a winform. I wish to login using a log and pass automatically via vb.net

I have the following code executing once the page loads

 WebBrowser1.Document.GetElementById("e").SetAttribute("value", "email@address")
                WebBrowser1.Document.GetElementById("p").SetAttribute("value", "password")
                WebBrowser1.Document.Forms(0).InvokeMember("submit")

I am able to fill in the boxes that need to contain the Log and pass but I am not able to fire off the login button of the webpage automatically. After research I am confident that I replicated the sample I found properly but obviously there is an issue. The 'button' isn't pressed because I don't get anything happening. When I press the website login manually it logs in correctly using the data that I filled in .

the webpage link is https://www.weatherzone.com.au/customise.jsp

and the relevant source data is below

 <form action="/customise.jsp" method="post" style="width: 75%; text-align: left;">
  <div style="margin-bottom: 1em;">
    <label for="e">Enter your email address:</label>
    <input style="width: 95%;" type="text" name="e" id="e" /><br />
  </div>

  <div style="margin-bottom: 1em;">
    <label for="p">Enter your password:</label>
    <input style="width: 95%;" type="password" name="p" id="p" /><br />
  </div>

  <div style="margin-bottom: 1em;">
    <input type="checkbox" name="rmbr" id="rmbr" value="1" />
    <label for="rmbr" style="display: inline;">Remember me (check this box if you are a regular user of this computer)</label>
  </div>

  <div style="text-align: right; margin-top: 20px; padding-top: 10px; border-top: 1px solid silver;">
    <input class="submit btn" type="submit" name="target" value="login" />
  </div>

  <input type="hidden" name="dest" value="/" />
</form>

解决方案

I applied this logic:

And it worked properly. It redirected to the same url and showed a red not authenticated message.

Code:

Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If (e.Url.ToString().StartsWith("https://www.weatherzone.com.au")) Then
        WebBrowser1.Document.GetElementById("e").SetAttribute("value", "email@address.com")
        WebBrowser1.Document.GetElementById("p").SetAttribute("value", "password")
        WebBrowser1.Document.All().GetElementsByName("target").Cast(Of HtmlElement).First().InvokeMember("click")
    End If
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.WebBrowser1.Navigate("https://www.weatherzone.com.au/customise.jsp")
End Sub

To make the code more stable, add required null checkings.

这篇关于如何以编程方式按下网站的登录按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆