[英]Utility to open a browser and click on specific link
我必須創建一個實用程序,該實用程序將打開特定站點的瀏覽器,然后單擊該頁面中存在的特定超鏈接。 我正在使用以下代碼創建一個控制台應用程序
try
{
System.Diagnostics.Process.Start("MyURL.aspx");
}
catch (Exception ex)
{
}
有人可以幫助您如何單擊上一頁中顯示的特定鏈接嗎?
我不想使用任何第三方工具,可以使用現有的.net庫完成某些工作嗎?
正如評論中所建議的那樣,使用WatiN
將使此操作變得更加容易:
using(var browser = new IE())
{
browser.GoTo("url");
}
要單擊特定鏈接:
browser.Link(Find.ById("someId")).Click();
嘗試將DOM附加到超級節點的HTML敏捷包,並在其中可以查詢隨后的提示-例如鏈接。
如果您不想使用任何外部工具,那么對於Internet Explorer,可以使用SHDocVw庫輕松完成。 這是一個打開Google頁面並單擊“登錄”鏈接的小示例。
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
ie.Visible = true;
ie.Navigate("https://www.google.co.in");
//Wait for page to load
while (ie.Busy)
{
Threading.Thread.Sleep(100);
}
dynamic allLinks = ((mshtml.IHTMLDocument3)ie.Document).getElementsByTagName("a");
foreach (mshtml.IHTMLAnchorElement link in allLinks) {
//Do some validation to find out the required link
if (link.href.Contains("https://accounts.google.com/ServiceLogin")) {
link.click();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.