简体   繁体   English

InvokeMember(“click”) webBrowser 帮助

[英]InvokeMember(“click”) webBrowser help

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.我试图通过 webBrowser 自动化一个网页,我试图点击的按钮没有 ID 只有一个值。 here's the html code for it:这是它的html代码:

<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.我不能使用GetElementById,因为按钮没有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");"我的脚本停止显示突出显示“goButton.InvokeMember("click");”的空引用错误

If I do如果我做

var inputControls = (from HtmlElement element in webBrowser1.Document.GetElementsByTagName("input") select element).ToList();
            HtmlElement submitButton = inputControls.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我的脚本在“HtmlElement submitButton”行给我一个“Sequence contains no matching element”错误,有时页面有多个接受按钮,所以我需要能够区分每个按钮或至少能够在不破坏脚本的情况下点击一个

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") .如果是这种情况, GetElementsByTagName("input")可能应该是GetElementsByTagName("button") The Name of the button isn't going to be Accept either, that'll be the InnerText property.按钮的名称也不会是 Accept,那将是InnerText属性。 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.如果您不确定该按钮是否存在,最好使用FirstOrDefault并检查空返回值,因为如果没有匹配项,'. First ' 将崩溃并烧毁。

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.不应有意引发 NullReferenceException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WebBrowser控件中的InvokeMember(“单击”) - InvokeMember(“click”) in WebBrowser control WebBrowser控件无法响应InvokeMember(“click”) - WebBrowser control not responding to InvokeMember(“click”) C# WebBrowser 控件 - 使用 InvokeMember("Click") 提交表单不工作 - C# WebBrowser Control - Form Submit Not Working using InvokeMember("Click") InvokeMember(“ click”)在线程内的webBrowser控件中不起作用 - InvokeMember(“click”) doesn't work in webBrowser control inside a thread btn.InvokeMember(“ click”)或btn.RaiseEvents(“ click”),其他任何情况都不会在Webbrowser上单击“确定” btn - btn.InvokeMember(“click”), or btn.RaiseEvents(“click”), and anything else won't click an 'OK' btn on webbrowser C# Webbrowser 文档,invokemember(“click”),是否可以等到单击操作解决? - C# Webbrowser document , invokemember(“click”), is it possible to wait until that click action is resolved? InvokeMember不适用于WPF Webbrowser - InvokeMember does not work with WPF Webbrowser C#WebBrowser HTMLElement.InvokeMember(“click”)无效 <form> 标签 - C# WebBrowser HTMLElement.InvokeMember(“click”) not working within <form> tag Awesomium中的InvokeMember(“ Click”) - InvokeMember(“Click”) in awesomium div中出现的InvokeMember(“ Click”) - InvokeMember(“Click”) as it come in div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM