所以我加载了一个网页,其中包含html代码:
<input style="margin-left: 140px;" name="e43X45asfaw4ybrZ34fi879234tg3e4eex" type="submit" id="submit" value="Begå kriminaliteten!" onmouseover="$('#ggg').fadeIn().delay(3000).fadeOut();">
我用它来点击它:
object o = webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("e43X45asfaw4ybrZ34fi879234tg3e4eex")[0].InvokeMember("click");
if (o != null)
{
MessageBox.Show("It worked!");
}
else
{
MessageBox.Show("It didnt work!");
}
并且该代码总是让它不起作用,它也没有对网页做任何事情。
这是我的完整代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MafiaspilletRankeBot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("mafiaspillet.no");
webBrowser1.ScriptErrorsSuppressed = true;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
timer1.Enabled = true;
timer1.Interval = 15000;
}
catch {
timer1.Enabled = false;
MessageBox.Show("Timer error", "Looks like there a error");
}
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
timerun.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("brukernavn")[0].SetAttribute("value", textBox1.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("passord")[0].SetAttribute("value", textBox2.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("login_buton")[0].InvokeMember("click");
timer2.Enabled = true;
timer2.Interval = 15000;
timer1.Enabled = false;
}
private void timer2_Tick(object sender, EventArgs e)
{
webBrowser1.Navigate("http://mafiaspillet.no/kriminalitet3.php");
timer3.Enabled = true;
timer3.Interval = 15000;
timer2.Enabled = false;
}
private void timer3_Tick(object sender, EventArgs e)
{
object o = webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("e43X45asfaw4ybrZ34fi879234tg3e4eex")[0].InvokeMember("click");
if (o != null)
{
MessageBox.Show("It worked!");
}
else
{
MessageBox.Show("It didnt work!");
}
MessageBox.Show("Bot finnished!", "YEEY!");
timer3.Enabled = false;
}
int i = 0;
private void runtime_Tick(object sender, EventArgs e)
{
i++;
timerun.Text = i.ToString() + " Sekunder";
}
}
}
所以我的问题是InvokeMember("click")
在我的public void timer3_Tick
中无效。
它无法做任何事情,但我无法找到问题:/
答案 0 :(得分:0)
您需要在函数WebBrowserDocumentCompletedEventArgs
中进行处理这里是完整代码
private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection inputCol = this.WebBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement el in inputCol)
{
if (el.GetAttribute("type").Equals("submit"))
{
el.InvokeMember("Click");
MessageBox.Show("It worked!");
}
}
}