C#使用selenium记录
selenium通过使用各种浏览器的驱动来操作浏览器,而浏览器的驱动则由各家浏览器自己提供和维护。
一、配置selenium环境
1. 使用NuGet搜索Selenium.WebDriver安装
2.安装自己在使用的浏览器驱动,我使用的是当前日期(2023年8月19日)下最新的Edge驱动
- 可以在NuGet中搜索MSEdgeDriver,安装到本地NuGet路径下,默认本地NuGet包存放路径为%userprofile%\.nuget\packages,当项目编译时,驱动“msdriver.exe”会自动复制到项目的/bin/debug目录下
- 也可以在edge官网https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/,下载自己所需的driver,放到debug目录下
- 如果以前已经下载过nuget包,IDE不会自动把包加载到此项目,需要手动在程序包管理器控制台运行:install-package 相应的包名
寻找已安装的包名:双击程序名后,出现包引用,即为包名

二、编写代码
1.创建driver实例,使用driver实例可以打开新网页,寻找网页元素做进一步操作,关闭浏览器等………
创建edgedriver一共需要两个参数:
- service:即使用的浏览器的驱动
- options:在操作浏览器时可使用的选项
service包含的常用方法
//service包含的常用方法
//隐藏黑色CMD窗口
service.HideCommandPromptWindow = true;
options包含的常用方法
//options包含的常用方法
//取消 EDGE正受到自动测试软件的控制的信息栏
options.AddExcludedArgument("enable-automation");
options.AddAdditionalOption("useAutomationExtension", false);
//禁用浏览器的保存密码选项
options.AddUserProfilePreference("credentials_enable_service", false);
//禁用浏览器弹窗(貌似不管用)
options.AddUserProfilePreference("profile.default_content_setting_values.notifications", 2);
// 不显示浏览器
options.AddArgument("--headless");
// GPU加速可能会导致Chrome出现黑屏及CPU占用率过高,所以禁用
options.AddArgument("--disable-gpu");
// 伪装user-agent
options.AddArgument("user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1");
//隐身模式(无痕模式)
options.AddArgument("--incognito");
// 设置chrome启动时size大小
options.AddArgument("--window-size=414,736");
// 禁用图片
options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
2. 寻找网页元素并打印
//css选择器: 选择所有list元素,条件是class属性的前面几个字符等于(^=) sky skyid的
//把所有list及其下属元素的集合赋给列表,Iwebelement数据类型规定,必须用只读列表存放
ReadOnlyCollection<IWebElement> weatherdays = driver.FindElements(By.CssSelector("li[class^='sky skyid'"));
foreach (IWebElement weather in weatherdays)
{
string date =weather.FindElement(By.TagName("h1")).Text;
string wea = weather.FindElement(By.ClassName("wea")).Text;
string tem = weather.FindElement(By.ClassName("tem")).Text;
string win = weather.FindElement(By.ClassName("win")).Text;
Console.WriteLine($"\n{date}天气信息:\n天气:{wea}\n气温:{tem}\n风力:{win}");
}
3. 完整源码:
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Support.UI;
namespace ConsoleApp2
{
public delegate double Calc(double x,double y);
internal class Program
{
static void Main(string[] args)
{
//@符号代表后面的转义字符不被处理
//驱动在编译时会自动copy到bin/debug文件夹下,所以写当前路径即可
EdgeDriverService service = EdgeDriverService.CreateDefaultService(@".","msedgedriver.exe");
EdgeOptions options = new EdgeOptions();
//隐藏黑色CMD窗口
service.HideCommandPromptWindow = true;
//不显示浏览器
options.AddArgument("--headless");
EdgeDriver driver = new EdgeDriver(service,options);
string URL = "http://www.weather.com.cn/weather/101010100.shtml";
driver.Navigate().GoToUrl(URL);
//css选择器: 选择所有list元素,条件是class属性的前面几个字符等于(^=) sky skyid的
//把所有list及其下属元素的集合赋给列表,Iwebelement数据类型规定,必须用只读列表存放
ReadOnlyCollection<IWebElement> weatherdays = driver.FindElements(By.CssSelector("li[class^='sky skyid'"));
foreach (IWebElement weather in weatherdays)
{
string date =weather.FindElement(By.TagName("h1")).Text;
string wea = weather.FindElement(By.ClassName("wea")).Text;
string tem = weather.FindElement(By.ClassName("tem")).Text;
string win = weather.FindElement(By.ClassName("win")).Text;
Console.WriteLine($"\n{date}天气信息:\n天气:{wea}\n气温:{tem}\n风力:{win}");
}
driver.Quit();
}
}
}

浙公网安备 33010602011771号