正常说, 需要安装的包
下面是所有的包
全部代码
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
using System;
using System.Windows.Forms;
using CefSharp.WinForms;
using CefSharp;namespace WindowsFormsApp2
{public partial class Form1 : Form{//cefpublic ChromiumWebBrowser chromiumwebBrowser;//selecniumpublic ChromeDriver driver;public Form1(){InitializeComponent();var settings = new CefSettings{Locale = "zh-CN"};settings.RootCachePath = AppDomain.CurrentDomain.BaseDirectory + "caches\\";settings.PersistSessionCookies = true;settings.PersistUserPreferences = true;settings.RemoteDebuggingPort = 9516;Cef.Initialize(settings);var AccountContext = new RequestContext(new RequestContextSettings(){CachePath = AppDomain.CurrentDomain.BaseDirectory + "caches\\global1\\",PersistSessionCookies = true,PersistUserPreferences = true});chromiumwebBrowser = new ChromiumWebBrowser("chrome://version/", AccountContext);BrowserSettings browserSettings = new BrowserSettings();chromiumwebBrowser.BrowserSettings = browserSettings;this.Controls.Add(chromiumwebBrowser);chromiumwebBrowser.Dock = DockStyle.Fill;}private void button1_Click(object sender, EventArgs e){var chromedriverservice = ChromeDriverService.CreateDefaultService("D:\\chromdriver");var options = new ChromeOptions();options.DebuggerAddress = "localhost:9516";driver = new ChromeDriver(chromedriverservice, options);driver.Navigate().GoToUrl("https://cn.bing.com/");driver.FindElement(By.Id("sb_form_q")).SendKeys("helloworld");driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);driver.FindElement(By.Id("search_icon")).Click();driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);driver.Quit();}}
}
需要注意的地方
1. selenium, 要下载3版本的。
2. 下载的驱动, 必须和cefsharp一致。
第一次写c#, 原以为网上应该有很多相关的资料了, 搜了半天都找不到, 自己研究了半天才弄出来。
以后自己写个脚本就方便了,需要的可以试试