InvokeMember("按一下[)web浏览器的帮助 [英] InvokeMember("click") webBrowser help

查看:331
本文介绍了InvokeMember("按一下[)web浏览器的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过自动化的weBrowser一个网页,我试图点击该按钮没有ID只有一个值。这里的HTML code吧:

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>

我不能useGetElementById的按钮没有ID。 如果我做

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

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

我的脚本停止呈现出nullreference错误突出goButton.InvokeMember(点击);

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

如果我这样做

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

这是序列中没有匹配的元素出错的HtmlElement提交按钮行我的剧本给我 有时候页面有一个以上的这些接受按钮,所以我需要能够告诉每个人之间的差异,以及或者至少能够点击一个没有剧本破

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

任何帮助,这将大大pciated AP $ P $

Any help with this will be greatly appreciated

推荐答案

您是要找到一个按钮的标签,是否正确?如果是这样的话,的getElementsByTagName(输入)大概应该是的getElementsByTagName(按钮)。按钮的名称是不会被接受要么,那将是对的的InnerText 财产。如果你不知道该按钮存在,你可能最好使用 FirstOrDefault 并检查空返回值,因为'。首先'会和好如初,如果有不匹配。

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.

这code是未经测试,但它可能是更接近你所需要的。

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
}

这篇关于InvokeMember(&QUOT;按一下[)web浏览器的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆