首先需要安裝套件
其一: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
以登入Netflix為例 使用以下範例
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
26
27
28
29
30
31
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 = "https://www.netflix.com/tw/Login";
driver.Navigate().GoToUrl(str);
IWebElement inputAccount = driver.FindElement(By.Name("userLoginId"));
Thread.Sleep(50);
inputAccount.SendKeys("Account"); //輸入帳號
Thread.Sleep(50);
IWebElement inputPassword = driver.FindElement(By.Name("password"));
Thread.Sleep(50);
inputPassword.SendKeys("Password"); //輸入密碼
Thread.Sleep(50);
//登入
IWebElement submitButton = driver.FindElement(By.XPath("//*[@class='btn login-button btn-submit btn-small']"));
Thread.Sleep(50);
submitButton.Click();
}
}
}
參數來源
執行GIF ,依照上面範例中的帳號密碼的輸入的話,理所當然會在Cick Login之後顯示帳號或密碼錯誤
額外需求補充 如果要點擊某個button,但是html都長一樣的話,像以下範例一樣,使用XPath彙整出陣列,在進行enter
1
2
IReadOnlyCollection<IWebElement> ConsoleCLI = driver.FindElements(By.XPath("//*[@class='console-controls ng-star-inserted']/button"));
ConsoleCLI.ToList()[3].SendKeys(Keys.Enter);