当前位置:网站首页>C#版Selenium操作Chrome全屏模式显示(F11)
C#版Selenium操作Chrome全屏模式显示(F11)
2022-07-06 09:33:00 【心蓝168】
最近开发一个教室智能控制软件,主要实现在讲台上对教室里的所有用电设备进行控制,其主要是基于MQTT实现的,其操控界面使用WEB页面实现。
讲台上的主控计算机开机后进入Windows桌面后,自动运行浏览器并进入操控页面。由于操控界面没有用户验证,所以需要隐藏操控界面的地址,所以对入口地址加了验证信息,然后转至真正的界面。为了看启动更像桌面软件,所以还要求要浏览器全屏显示(即:自动按下F11进入全屏模式)。
方法一:通过创建Chrome的快捷方式,并在参数中加入--kiosk取可实现,如下图所示:

然后将其加入到Windows的开机启动项中即可。
该方法的唯一的缺点是:无法真正的隐藏操控界面的入口地址,对懂点计算机的人而然都能轻易拿到该地址。采用其他设备访问该地址对设备进行操作,这是我们不想看到的。
方法二:使用自动化测试工具Selenium实现对Chrome浏览器的控制。下载Selenium相关类库,及下载与本地计算机所安装的Chrome浏览器版本一致的ChromeDriver.exe。下载地址是:http://chromedriver.storage.googleapis.com/index.html
控制浏览器全屏的方法是对ChromeOptions的属性进行设置。网上很多人发的贴都是将浏览器的最大化写作浏览器的全屏,其实这是很不对。正确的做法如下所示:

哈哈,上图看到不清楚吧,我再附上代码:
#region 启动浏览页面
private void StartWebBroswer()
{
try
{
var option = new ChromeOptions();
//是否隐藏浏览器
//如果注释掉的话,那么会在启动的时候打开谷歌浏览器,在页面上可以看到填单的操作,如果取消的话,就只有cmd窗口,谷歌浏览器隐匿运行
//option.AddArgument("headless"); //无头模式(即浏览器隐藏运行)
//option.AddArgument("window-size=1024,768"); //指定浏览器尺寸
if (this.ViewKiosk == "1")
{
option.AddArgument("--kiosk"); //全屏(但按F11无法退出全屏)
}
else
{
option.AddArgument("--start-fullscreen"); //全部(按F11可以退出全屏)
}
//option.AddArgument("--start-maximized"); //浏览器最大化
option.AddExcludedArgument("enable-automation");//不显示【正受到自动测试软件控制】字样
//option.AddAdditionalCapability("useAutomationExtension", false); //
//设置cmd窗口不弹出来
ChromeDriverService service = ChromeDriverService.CreateDefaultService(System.AppDomain.CurrentDomain.BaseDirectory.ToString());
service.HideCommandPromptWindow = true;
this.DriverObj = new ChromeDriver(service, option);
this.DriverObj.Navigate().GoToUrl(this.ViewUrl);//https://www.baidu.com
this.ExecutorObj = (IJavaScriptExecutor)DriverObj;
this.ActionObj = new Actions(DriverObj);
}
catch (Exception xe)
{
this.WriteLogToFile("启浏览器失败,原因:" + xe.Message);
}
}
#endregion上面的代码段还包括了控制浏览器的无头模式,指定浏览器的尺寸、浏览器最大化、不显示【正受到自动测试软件控制】字样等,大家可以尽情使用。
下面是本次项目开发的一个小工具,在Windows的触屏设备上特别适用,支持随意设定启动的地址,开机自动运行、信息亭模式及密码保护等,供大家参考。


下载地址:
Chrome浏览器全屏显示并加载指定页面-桌面系统文档类资源-CSDN下载
边栏推荐
- Only learning C can live up to expectations Top1 environment configuration
- Compile homework after class
- 微信防撤回是怎么实现的?
- Log4j2 major vulnerabilities and Solutions
- SQL tuning notes
- 汇编语言寻址方式
- Resume of a microservice architecture teacher with 10 years of work experience
- Typescript basic operations
- JVM 垃圾回收器之Serial SerialOld ParNew
- Mongodb learning notes
猜你喜欢

"One year after graduation, I won ACL best paper"

复盘网鼎杯Re-Signal Writeup

Activiti目录(三)部署流程、发起流程

The daemon thread starts redis and modifies the configuration file

Notes on how the network is connected

MySQL日期函数

肖申克的救赎有感

Thank you for your invitation. I'm in the work area. I just handed in the code. I'm an intern in the next ByteDance

JVM garbage collector part 2

Alibaba cloud server builds SVN version Library
随机推荐
MySQL字符串函数
Use of mongodb in node
Control transfer instruction
The daemon thread starts redis and modifies the configuration file
High performance mysql (Third Edition) notes
Flink 解析(一):基础概念解析
一个数10年工作经验的微服务架构老师的简历
vscode
【逆向】脱壳后修复IAT并关闭ASLR
吴军三部曲见识(七) 商业的本质
JVM之垃圾回收器上篇
唯有學C不負眾望 TOP5 S1E8|S1E9:字符和字符串&&算術運算符
暑假刷题嗷嗷嗷嗷
Set up the flutter environment pit collection
信息与网络安全期末复习(基于老师给的重点)
In the command mode in the VI editor, delete the character usage at the current cursor__ Command.
【逆向中级】跃跃欲试
Instructions for Redux
On the clever use of stream and map
JVM garbage collection overview