正常说, 需要安装的包
下面是所有的包
全部代码
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
{
//cef
public ChromiumWebBrowser chromiumwebBrowser;
//selecnium
public 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#, 原以为网上应该有很多相关的资料了, 搜了半天都找不到, 自己研究了半天才弄出来。文章来源:https://www.toymoban.com/news/detail-660874.html
以后自己写个脚本就方便了,需要的可以试试文章来源地址https://www.toymoban.com/news/detail-660874.html
到了这里,关于winform中嵌入cefsharp, 并使用selenium控制的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!