1

我正在尝试以编程方式单击 HTML 网页上的一系列按钮,如下所示:

<div class="srp-actions blue-button"><a class="primary-action-button label" href="/people/invite?from=profile&amp;key=243930744&amp;firstName=Will&amp;lastName=Yang&amp;authToken=p8Oz&amp;authType=OUT_OF_NETWORK&amp;connectionParam=member_desktop_search_people-vertical-module&amp;csrfToken=ajax%3A7824954558998584370&amp;trk=vsrp_people_res_pri_act&amp;trkInfo=VSRPsearchId%3A12487701484818103943%2CVSRPtargetId%3A243930744%2CVSRPcmpt%3Aprimary" data-li-result-interaction="instant-connect" data-li-success-text="Invite Sent" data-li-connect-href="/people/contacts-search-invite-submit?memIds=243930744&amp;authTokens=p8Oz&amp;authTypes=OUT_OF_NETWORK&amp;from=voltron&amp;firstName=Will&amp;lastName=Yang&amp;isAjax=true&amp;connectionParam=member_desktop_search_people-vertical-module&amp;csrfToken=ajax%3A7824954558998584370&amp;trk=vsrp_people_res_invite_act&amp;trkInfo=VSRPsearchId%3A12487701484818103943%2CVSRPtargetId%3A243930744%2CVSRPcmpt%3Aprimary">Connect</a><div class="secondary-actions-trigger"><button role="button" class="trigger"><span>Secondary Actions</span></button><ul class="menu"><li><a href="/requestList?displayProposal=&amp;destID=243930744&amp;creationType=DC&amp;authToken=p8Oz&amp;authType=OUT_OF_NETWORK&amp;trk=vsrp_people_res_sec_act&amp;trkInfo=VSRPsearchId%3A12487701484818103943%2CVSRPtargetId%3A243930744%2CVSRPcmpt%3Aprimary">Send InMail</a></li><li><a href="/forwardProfileMsg?displayCreate=&amp;profileID=0_0TL7XobJNwUxDDGHun9drH&amp;profileName=Will+Yang&amp;network=I&amp;trk=vsrp_people_res_sec_act&amp;trkInfo=VSRPsearchId%3A12487701484818103943%2CVSRPtargetId%3A243930744%2CVSRPcmpt%3Aprimary">Share</a></li></ul></div></div>

这是查找按钮元素并执行操作的当前代码:

HtmlElementCollection elements = webBrowser1.Document.GetElementsByTagName("a");

// First find and click "Connect" buttons
foreach (HtmlElement item in elements)
{
    if (item.OuterHtml.Contains("action-button label") && 
        !item.OuterHtml.Contains("Message") &&
        item.OuterHtml.Contains("OUT_OF_NETWORK"))
    {
        item.SetAttribute("href", item.GetAttribute("data-li-connect-href"));
        item.InvokeMember("Click");
    }
}

代码正确地找到了锚元素,但 InvokeMember 方法似乎没有产生任何结果,知道有什么问题吗?

4

1 回答 1

2

您使用的标签并未准确指定您需要哪个特定标签:

x = webBrowser1.Document.GetElementsByTagName("a")

而是尝试:

x= webBrowser1.Document.GetElementsById("anchor_id");
x.InvokeMember("click");

或者尝试使用以下方法来验证它是否是您通过属性使用的预期标签。

if (element.GetAttribute(attribute).Equals(attName))
于 2017-01-19T09:43:23.957 回答