開啟新的Visual Studio專案,會使用以下套件
其一:Selenium.WebDriver
1
NuGet\Install-Package Selenium.WebDriver -Version 4.5.1
其二:Selenium.WebDriver.ChromeDriver ( 版本需依據本地Chrome的版本微調)
1
NuGet\Install-Package Selenium.WebDriver.ChromeDriver -Version 106.0.5249.6100
新增AutoIT
開啟AutoIT
AutoIT使用以下Code匯出exe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <Constants.au3>
;RequireAdmin ; unsure if it's needed
;$iSleep = 2000
Opt("WinSearchChildren", 1)
$sUsername = "Account"
$sPassword = "Password"
Sleep(1000)
For $i = 1 To 20 Step 1
Sleep(3000)
$sTitle = WinGetTitle("Sign in")
If $sTitle = "strTitle" or WinWaitActive("[CLASS:Chrome_WidgetWin_1]") or WinWaitActive("Sign in") Then
Send($sUsername)
Send("{TAB}")
Send($sPassword,1);$SEND_RAW (1)
Send("{TAB}")
Send("{ENTER}")
Exit 0
Else
ContinueLoop
EndIf
Next
Exit 1
匯出exe的方式是在Tool底下選擇Go or Compile 匯出後可以在同目錄底下看到檔案
AutoIT的Code參數來源
開啟新專案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Threading;
namespace WebBrowser_AutoLogin
{
internal class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
string str = "URL_Path"; //輸入會跳出Winodows驗證的網址
driver.Navigate().GoToUrl(str);
IWebElement inputAccount = driver.FindElement(By.Name("userLoginId"));
Thread.Sleep(2000);
Process.Start("輸入匯出的exe的Path");//使用上面匯出的Exe來處理密碼輸入問題
//登入
IWebElement submitButton = driver.FindElement(By.XPath("//*[@class='btn login-button btn-submit btn-small']"));//自行變更需要進行的操作
submitButton.Click();
}
}
}
如果開啟得網頁是http可以使用以下指令點選進階,並進入網頁(自行變更id裡面的內容)
1
2
3
4
5
Thread.Sleep(50);
driver.FindElement(By.Id("details-button")).SendKeys(Keys.Enter);
// IWebElement submitButton = driver.FindElement(By.XPath("//*[@class='secondary-button small-link']"));
Thread.Sleep(50);
driver.FindElement(By.Id("proceed-link")).SendKeys(Keys.Enter);