栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

各系统剪切板内容获取

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

各系统剪切板内容获取

最近在做UI自动化 出现点击按钮复制文案到剪切板的操作 由于执行机器有mac、win10、liunx 出现不同的兼容问题 简单记录一下

1、常用获取方式 Python xerox模块
import xerox
xerox.copy(str) # 复制内容到剪切板
xerox.paste() # 读取剪切板内容
# Liunx可以选择粘贴到xsel中
xerox.copy(str, xsel True)
xerox.paste(xsel True)
Python pyperclip模块
import pyperclip
pyperclip.copy(str) # 复制内容到剪切板
pyperclip.paste() # 读取剪切板内容
命令行获取 Windows
复制
clip file.txt
echo file.txt | clip
clip file.txt # 写入文档
powershell -command Get-Clipboard # 直接输出到命令行
Mac
复制
pbcopy file.txt
cat file.txt | pycopy
pbpaste file.txt # 写入文档
pbpaste # 直接输出到命令行
Liunx
复制 到 gnome 的剪贴板 
xsel file.txt
cat file.txt | xsel
xsel --input --clipboard # copy to clipboard
xsel file.txt # 写入文档
xsel --output --clipboard # get from clipboard
Ubuntu
sudo apt-get install xclip # 需要先安装xclip
复制 到 gnome 的剪贴板 
xclip -selection file.txt
cat file.txt | xclip -selection
xclip file.txt
2、遇坑

由于UI自动化是selenium chrome 出现不兼容问题

2.1 mac

chrome页面模式/headless模式 均可正常获取

2.2 win

chrome页面模式可以正常获取 headless模式不能写入系统剪切板

发现每次返回的都是上一次复制的内容使用xerox、pyperclip包均不可行尝试在用例执行过程中不停执行命令行 发现headless模式下不会将复制到的内容写入剪切板 2.3 centos

无法获取系统剪切板

使用xerox包获取 获取到的剪切板内容为None使用命令行xsel --output --clipboard获取 仍然返回None使用pyperclip包报错PyperclipException:Pyperclip could not find a copy/paste mechanism for your system. 搜索需要安装xsel xclip 后续安装无果 解决方式

作为英语渣渣熟练的command v复制文本到百度翻译 突然冒出一个想法“管你什么系统ctrl v粘贴没毛病” 开搞

 from selenium.webdriver import ActionChains
 from selenium.webdriver.common.keys import Keys
 from selenium.webdriver.support.wait import WebDriverWait
 from selenium.common.exceptions import TimeoutException
 self.driver.execute_script( window.open( ) ) # 打开一个新窗口
 self.driver.switch_to.window(self.driver.window_handles[-1]) # 进入新窗口
 self.driver.get(url https://fanyi.baidu.com/ ) # 访问百度翻译
 exit True
 try:
 WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, desktop-guide-close ))) # 发现会有个广告弹窗 强制等待检查下是否出现弹窗
 except TimeoutException:
 exit Flase
 if exit True:
 self.driver.find_element(*(By.CLASS_NAME, desktop-guide-close )).click() # 点击广告弹窗X按钮
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267925.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号