开发者

InvokeMember("click") webBrowser help

开发者 https://www.devze.com 2022-12-27 17:24 出处:网络
I am trying to automate a web page via the weBrowser and the button that i\'m trying to click has no ID only a value. here\'s the html code for it:

I am trying to automate a web page via the weBrowser and the button that i'm trying to click has no ID only a value. here's the html code for it:

<td width='10%' align='center'><button class="buttonf" onclick="window.location='staticpage.php?accept=123456' ">Accept</button></td>

I can't useGetElementById as the button has no ID. If I do

HtmlElement goButton = this.webBrowser1.Document.All["Accept"];
goButton.InvokeMember("click");

My script stops showing a nullreference error highlighting the "goButton.InvokeMember("click");"

If I do

var inputControls = (from HtmlElement element in webBrowser1.Document.GetElementsByTagName("input") select element).ToList();
            HtmlElement submitButton = inputContr开发者_StackOverflowols.First(x => x.Name == "Accept");

My script give me an "Sequence contains no matching element" error at the "HtmlElement submitButton" line and sometimes the page has more than one of these Accept buttons, so I would need to be able to tell the difference between each one as well or at least be able to click on one without the script breaking

Any help with this will be greatly appreciated


You're trying to find a button tag, correct? If that's the case, GetElementsByTagName("input") probably should be GetElementsByTagName("button"). The Name of the button isn't going to be Accept either, that'll be the InnerText property. If you're not sure that the button exists, you're probably better off using FirstOrDefault and checking for a null return value, as '.First' will crash and burn if there's no match.

As for how you can tell the difference between the buttons if there's more than one Accept button, I think that one needs a bit more thought.

This code is untested, but it might be closer to what you need

var buttonControls = (
    from HtmlElement element 
    in webBrowser1.Document.GetElementsByTagName("button") 
    select element
).ToList();

HtmlElement submitButton = 
    buttonControls.FirstOrDefault(x => x.InnerText == "Accept");

if (submitButton != null) {
    submitButton.InvokeMember("click");
} else {
    //didn't find it
}


Looks like there is no matching element in the page. Are you sure the button is on the current page, not in an embedded frame?

Anyway you need to have error checking. NullReferenceException should not be intentionally raised.

0

精彩评论

暂无评论...
验证码 换一张
取 消