当前位置: 首页 > news >正文

C#使用Selenium驱动Chrome浏览器

1.Selenium库依赖安装

Selenium WebDriver是Selenium项目的一部分,用于模拟用户在Web应用程序中的交互操作。它支持多种浏览器,如Chrome、Firefox、IE等,且与各种编程语言(如Java、Python、C#等)兼容,具有广泛的适应性。

Selenium WebDriver的核心原理通过向浏览器发送命令和脚本,模拟真实用户的操作行为,从而实现对Web应用程序的自动化测试

2.准备材料

        最新版本chromedriver 下载地址:
https://googlechromelabs.github.io/chrome-for-testing/#stable

        这里有自动测试用到的chrome版本以及驱动。

Chrome for Testing availability 

         最新的Chrome版本和驱动:

下载后Chorme的目录如下图:

 ChromeDriver如下:

3.示例代码

         ChromeDriver

                        string chromiumFolderLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\chromium";chromeOptions = new ChromeOptions{UnhandledPromptBehavior = UnhandledPromptBehavior.Accept,};// check if in incog mode, if it is, then we launch incog modeif (this.incogMode){chromeOptions.AddArgument("--incognito");}// enable headless modeif (this.headless){Logger.Info("Started a headless session");chromeOptions.AddArgument("--headless=new");}chromeOptions.AddArgument("--start-maximized");chromeOptions.AddArgument("no-sandbox");chromeOptions.AddArgument("--log-level=3");chromeOptions.AddArgument("--silent");chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);chromeOptions.AddUserProfilePreference("download.default_directory", pathToNewFolder);chromeOptions.AddUserProfilePreference("disable-popup-blocking", true);chromeOptions.AddUserProfilePreference("plugins.always_open_pdf_externally", true);chromeOptions.BinaryLocation = $"{chromiumFolderLocation}\\chrome.exe";// we want to find all the files under the location of chromium\Extensions and add them in.if (Directory.Exists(chromiumFolderLocation + "\\Extensions")){foreach (string extension in Directory.GetFiles(chromiumFolderLocation + "\\Extensions")){chromeOptions.AddExtension(extension);}}var pp =  Environment.OSVersion.Platform; service = ChromeDriverService.CreateDefaultService(this.seleniumDriverLocation);service.SuppressInitialDiagnosticInformation = true;this.WebDriver = new ChromeDriver(service,chromeOptions);

备注

        1.调试过程中遇到的一个常数Environment.OSVersion.Platform值是“Win32NT”,开发环境操作系统是win10_64位的。

        2.Headerless Browser(无头的浏览器)是浏览器的无界面状态,可以在不打开浏览器GUI的情况下,使用浏览器支持的性能。

http://www.lryc.cn/news/331219.html

相关文章:

  • 【软件工程】详细设计(二)
  • 数据质量决定大模型能力,景联文科技提供高质量大模型数据
  • 大话设计模式之状态模式
  • 【机器学习】“强化机器学习模型:Bagging与Boosting详解“
  • 《QT实用小工具·九》设备按钮控件
  • 计算机服务器中了helper勒索病毒怎么办,helper勒索病毒解密流程步骤
  • qT 地图显示飞机轨迹
  • 智慧展览馆:基于AI智能识别技术的视频智慧监管解决方案
  • Linux实用性脚本 [bash]
  • Key exchange failed.No compatible key exchange method.
  • 【FAQ】HarmonyOS SDK 闭源开放能力 —Asset Store Kit
  • arm架构离线部署docker
  • SpringBoot(48)-使用 SkyWalking 进行分布式链路追踪
  • JS实现双向数据绑定的三种方式
  • Bash相关
  • Flink 流批一体在模型特征场景的使用
  • 06-编辑器
  • 5.3.2 实验2:配置交换机端口安全
  • 【AIGC调研系列】通义千问、文心一言、抖音云雀、智谱清言、讯飞星火的特点分析
  • 【JVM】如何定位、解决内存泄漏和溢出
  • 常见网络问题的概述
  • 说说你对数据结构-树的理解
  • Docker实例
  • python基础——模块【模块的介绍,模块的导入,自定义模块,*和__all__,__name__和__main__】
  • 【HTML】标签学习(下.2)
  • os模块篇(十一)
  • 编译amd 的 amdgpu 编译器
  • github 多个账号共享ssh key 的设置方法
  • dm8修改sysdba用户的密码
  • 基于boost准标准库的搜索引擎项目