简体   繁体   English

如何单击没有ID的网页上的按钮?

[英]How to click a button on a web page that has no id?

If you navigate to this website , you can see there is an ExportExcel button. 如果导航到该网站 ,则可以看到一个ExportExcel按钮。 If I view source , I find the button under this format: 如果查看source ,则会找到以下格式的按钮:

<td align="right" class="ExportExcel" valign="middle">                                    
    <a href="JavaScript:void(0)" onClick="openExport('../pages/ListExportToExcel.aspx?zipCode=&city=&county=&sState=MI&fromPrice=0&toPrice=0&fCaseNumber=&bed=0&bath=0&street=&buyerType=0&specialProgram=&Status=0&indoorAmenities=&outdoorAmenities=&housingType=&stories=&parking=&propertyAge=');return false;" >Export to</a>
</td>

Following this solution : 按照此解决方案

WebBrowser MyBrowser = new WebBrowser();
MyBrowser.Navigate("https://www.hudhomestore.com/Listing/PropertySearchResult.aspx?sState=MI");
HtmlElementCollection classButton = MyBrowser.Document.All;
foreach (HtmlElement element in classButton)
    if (element.GetAttribute("ExportExcel") == "button")
        element.InvokeMember("click");

I am getting an error as MyBrowser.Document is null: 我收到错误消息,因为MyBrowser.Document为null:

Object reference not set to an instance of an object. 你调用的对象是空的。

Where am I going wrong? 我要去哪里错了? Or is there a better / different way? 还是有更好/不同的方式?

EDIT: 编辑:

Based on the s uggestion by user @DavidR , I have tried the below but MyBrowser_DocumentCompleted never gets any hits: 基于S 用户@DavidR uggestion ,我曾尝试以下,但MyBrowser_DocumentCompleted从来没有得到任何点击次数:

public partial class mainForm : Form
{
    WebBrowser MyBrowser = new WebBrowser();

    // ..

    private void mainForm_Load(object sender, EventArgs e)
    {
        MyBrowser.Navigate("https://www.hudhomestore.com/Listing/PropertySearchResult.aspx?sState=MI");
    }

    void MyBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        HtmlElementCollection classButton = MyBrowser.Document.All;
        foreach (HtmlElement element in classButton)
            if (element.GetAttribute("ExportExcel") == "button")
                element.InvokeMember("click");
    }

}

Get all the Anchor tags and find your required tag which you want to click. 获取所有的Anchor tags然后找到要单击的所需tag I've made a code, try this out. 我已经编写了代码,请尝试一下。

        HtmlElementCollection links = MyBrowser.Document.GetElementsByTagName("A");
        foreach (HtmlElement link in links)
        {
            if (link.InnerText!=null && link.InnerText.Equals("Export to"))
                link.InvokeMember("Click");
        }

Hope it helps. 希望能帮助到你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM