问题
Let's say that I've got the following HTML:
<div id="active_server">
<div class="clearfix">
<a href="#" onclick="return Index.submit_login('server_nlp1');">
<span class="world_button_active">Server NL P1</span>
</a>
<a href="#" onclick="return Index.submit_login('server_nlp2');">
<span class="world_button_active">Server NL P2</span>
</a>
</div>
</div>
Now let's say that i want to simulate an click on server NL P2. I know that I've got to get the HTML. so first i get the div like this:
HtmlElement active_server = webBrowser1.Document.GetElementById("active_server").GetElementsByTagName("div")[0];
But then is my question as follow:
How do i loop through the elements and search in the Onclick
wich link I've got to click ( note, the onclick's are variable, so i can't do an index and click or something like that. I really need to compare the onclick or the text in the span)
I'm very new to this type of programming.
EDIT 1 I don't really know on what place the element is because it can change ( sometimes there are more servers, and i've got to find the right server...)
EDIT 2
The HTML looks like this:
<div id="active_server">
<p class="pseudo-heading">Op welke wereld wil je je aanmelden?</p>
<div class="clearfix">
<a href="#" onclick="return Index.submit_login('server_nl33');">
<span class="world_button_active">Wereld 33</span>
</a>
<a href="#" onclick="return Index.submit_login('server_nlp1');">
<span class="world_button_active">Casual</span>
</a>
</div>
<div class="clearfix">
<a href="#" onclick="return Index.submit_login('server_nl34');">
<span class="world_button_inactive">Wereld 34</span>
</a>
</div>
<p class="pseudo-heading" id="show_all_server">
<a href="#" onclick="$('#show_all_server').hide();$('#inactive_server_list').show();return false">Toon alle werelden</a>
</p>
</div>
回答1:
My guess: you're trying to find what A
link to click by the text, contained in the inner SPAN
of that link.
Try this:
HtmlElement active_server = webBrowser1.Document.GetElementById("active_server").GetElementsByTagName("div")[0];
HtmlElement link = null;
if (FindLinkToClick(active_server, "Server NL P1", ref link)
{
link.InvokeMember("click", null);
}
// FindLinkToClick
bool FindLinkToClick(HtmlElement root, string text, ref HtmlElement found)
{
foreach (var child in root.Children)
{
var element = (HtmlElement)child;
if (element.InnerText == text)
{
found = element.Parent;
return true;
}
if (FindLinkToClick(element, text, ref found))
return true;
}
return false;
}
[EDITED]
If you're sure the string you're looking for (e.g, "Server NL P1") is unique, you can just use webBrowser1.WB.Document.Body
as the root
for search:
HtmlElement link = null;
if (FindLinkToClick(webBrowser1.WB.Document.Body, "Server NL P1", ref link))
{
link.InvokeMember("click", null);
}
[EDITED]
I just tested it with the below HTML and it works. Note <a href="#" onclick="alert('clicked!')">
, I do see that "clicked!" alert.
<div id="active_server">
<p class="pseudo-heading">Op welke wereld wil je je aanmelden?</p>
<div class="clearfix">
<a href="#" onclick="alert('clicked!')">
<span class="world_button_active">Wereld 33</span>
</a>
<a href="#" onclick="return Index.submit_login('server_nlp1');">
<span class="world_button_active">Casual</span>
</a>
</div>
<div class="clearfix">
<a href="#" onclick="return Index.submit_login('server_nl34');">
<span class="world_button_inactive">Wereld 34</span>
</a>
</div>
</div>
I call it like this:
HtmlElement link = null;
if (FindLinkToClick(this.webBrowser1.Document.Body, "Wereld 33", ref link))
link.InvokeMember("click", null);
[UPDATE]
The code works unmodified with the link your provided. Maybe, you need to make sure the document is fully loaded, like this:
bool _loaded = false;
public Form1()
{
InitializeComponent();
this.WB.DocumentCompleted += WB_DocumentCompleted;
}
void WB_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (_loaded)
return;
_loaded = true;
HtmlElement link = null;
if (FindLinkToClick(this.WB.Document.Body, "Wereld 33", ref link))
{
link.InvokeMember("click", null);
}
}
void Form1_Load(object sender, EventArgs e)
{
this.WB.Navigate(@"http://scriptsenprogs.nl/twmc.html");
}
回答2:
webBrowser1.DocumentCompleted += (s, e) =>
{
HtmlElement active_server = webBrowser1.Document.GetElementById("active_server").GetElementsByTagName("div")[0];
var a = active_server.GetElementsByTagName("a")[0];
a.InvokeMember("click", null);
};
来源:https://stackoverflow.com/questions/18176871/webbrowser-click-on-link-with-onclick-attribute