Selenium 使用 Chromedriver 测试打包后的 electron 应用

苏打 · 2018年04月20日 · 最后由 wanghua 回复于 2022年10月17日 · 2623 次阅读

安装 Selenium

pip install selenium

引入 ChromeOptions

打包后的 electron app 一般是这样的结构

electron
├── ElectronApp.exe
electron/resources/app 
├── package.json 
├── main.js 
└── index.html

script 中在初始化 chromedriver 的时候引入 ChromeOptions 对象并对 binary_location 进行赋值

options = webdriver.ChromeOptions()
options.binary_location = r"c:\ElectronApp.exe" # 这里就是打包后的electron

启动 Webdriver

启动 webdriver 的时候将 ChromeOptions 作为参数传入

browser = webdriver.Chrome(chromedriver, options=options)

这时候 electron 的 app 会被调起来,接下来即可对 eletron 中的对象执行操作了,和其它的 webdriver 写法一样没什么不同,这里不详述了。

参考文档

https://electronjs.org/docs/tutorial/using-selenium-and-webdriver

共收到 10 条回复 时间 点赞

想问一下,启动起来拿不到网页下的 html 代码

报错这个,楼主怎么解决

大哥 在么 Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: DevToolsActivePort file doesn't exist 报这个错 能帮忙解决下么 100 块钱行么

wengaou 回复

1.Selenium 启动参数加个 --no-sandbox
2.electron 壳如果用了 remote-debugging-port 给注释掉
试一下看看

YueChen 回复

您好 可以加个微信么

您好,我有遇到了一个问题,需要请教。
我在用 selenium 启动了 electron 的 app 以后, 后面的代码好像就执行不下去了,甚至不能查看 title 或者 print 一些信息。

想请问一下您有没有遇到这样的问题,如何解决的。
系统是:
mac: 10.14.6
python: 3.7
chrome: 79.0.3945.88
webdriver: ChromeDriver 79.0.3945.36

也尝试了用 firefox, 但是遇到了同样的问题。

wengaou 回复

解决了吗?

selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist

如何解决啊?

可以加的微信吗

driver = webdriver.Chrome(driver_path, options=options) 我这一步就报错了 报错信息 DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(driver_path, options=options)

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册