内容说明
本文主要基于ChromeDriver实现模拟手机UI截图功能,完整项目结构请参考
https://blog.youkuaiyun.com/lylfv/article/details/106874925
测试环境
- CentOS 7
- Python 3
- Chrome & ChromeDriver
Chrome 浏览器安装
- 编辑repo文件
cd /etc/yum.repos.d/
vim google-chrome.repo
内容如下:
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64
enabled=1
gpgcheck=1
gpgkey=https://dl.google.com/linux/linux_signing_key.pub
- 安装命令
yum -y install google-chrome-stable --nogpgcheck
- 查看版本
google-chrome --version
ChromeDriver下载&配置
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/
chmod 755 /usr/bin/chromedriver
代码详情
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
def capture_screen():
# 打开浏览器并登陆
mobileEmulation = {'deviceName': 'Galaxy S5'}
options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', mobileEmulation)
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
browser = webdriver.Chrome(executable_path="/usr/bin/chromedriver", options=options)
browser.get('https://www.baidu.com/')
browser.save_screenshot('screen.png')
browser.quit()
if __name__ == "__main__":
capture_screen()
输出结果
- 保存上述代码为ui_test.py
- 当前目录运行命令
python3 ui_test.py
生成screen.png文件

本文介绍如何使用ChromeDriver在CentOS7环境下,通过Python模拟Galaxy S5设备进行UI截图。涉及Chrome及ChromeDriver的安装配置,以及selenium库的具体应用。
416

被折叠的 条评论
为什么被折叠?



