[英]Programmatically click button in C#
我試圖以編程方式單擊HTML網頁上的一系列按鈕,如下所示:
<div class="srp-actions blue-button"><a class="primary-action-button label" href="/people/invite?from=profile&key=243930744&firstName=Will&lastName=Yang&authToken=p8Oz&authType=OUT_OF_NETWORK&connectionParam=member_desktop_search_people-vertical-module&csrfToken=ajax%3A7824954558998584370&trk=vsrp_people_res_pri_act&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&authTokens=p8Oz&authTypes=OUT_OF_NETWORK&from=voltron&firstName=Will&lastName=Yang&isAjax=true&connectionParam=member_desktop_search_people-vertical-module&csrfToken=ajax%3A7824954558998584370&trk=vsrp_people_res_invite_act&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=&destID=243930744&creationType=DC&authToken=p8Oz&authType=OUT_OF_NETWORK&trk=vsrp_people_res_sec_act&trkInfo=VSRPsearchId%3A12487701484818103943%2CVSRPtargetId%3A243930744%2CVSRPcmpt%3Aprimary">Send InMail</a></li><li><a href="/forwardProfileMsg?displayCreate=&profileID=0_0TL7XobJNwUxDDGHun9drH&profileName=Will+Yang&network=I&trk=vsrp_people_res_sec_act&trkInfo=VSRPsearchId%3A12487701484818103943%2CVSRPtargetId%3A243930744%2CVSRPcmpt%3Aprimary">Share</a></li></ul></div></div>
這是用於查找button元素並執行操作的當前代碼:
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");
}
}
代碼正確地找到了anchor元素,但是InvokeMember方法似乎沒有產生任何結果,知道什么地方出錯了嗎?
您使用的標簽未完全指定您需要的特定標簽:
x = webBrowser1.Document.GetElementsByTagName("a")
而是嘗試:
x= webBrowser1.Document.GetElementsById("anchor_id");
x.InvokeMember("click");
或嘗試使用以下方法來驗證您通過via屬性使用的預期標簽。
if (element.GetAttribute(attribute).Equals(attName))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.