【发布时间】:2015-12-23 09:05:31
【问题描述】:
在这段代码中,我使用 webbrowser 导航到一个站点。 然后我想在网站上点击一个按钮。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web.UI;
namespace Click_On_Website_Button
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate("http://www.mysiteexample.com");
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection classButton = webBrowser1.Document.All;
foreach (HtmlElement element in classButton)
{
if (element.GetAttribute("addMessage") == "button")
{
element.InvokeMember("click");
}
}
}
}
}
当我在查看页面源代码时,我搜索 addmessage 并找到:
http://www.mysiteexample.com
但是当我运行我的程序时,它永远不会到达这条线:
element.InvokeMember("click");
当我在按钮上的网站上制作 Inspect 元素以添加消息时,它在此:
<span class="addMessage" onclick="location='http://www.tapuz.co.il/forums/addmsg/393/טבע_ומזג_אוויר/מזג_האוויר'"> | הוספת הודעה</span>
在源代码视图中,addmessage 部分是这样的:
<div class="SecondLineMenu" id="SecondLineMenu">
<span class="addMessage" onclick="location='http://www.tapuz.co.il/forums/addmsg/393/טבע_ומזג_אוויר/מזג_האוויר'" > | הוספת הודעה</span>
【问题讨论】: