各系统剪切板内容获取

最近在做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按钮
        	
        
        self.driver.find_element(*(By.ID, "baidu_translate_input")).click()  # 点击输入框
        ac = ActionChains(self.driver)  # 模拟键盘操作
        if self.platform == 'MacOS':  # mac为command+v
            ac.key_down(Keys.COMMAND).send_keys('v').perform()
        elif self.platform == 'Windows':  # win为control+v
            ac.key_down(Keys.CONTROL).send_keys('v').perform()
        elif self.platform == 'Linux':  # liunx为control+shift+v
            ac.key_down(Keys.CONTROL).key_down(Keys.SHIFT).send_keys('v').perform()
        result = self.driver.find_element(*(By.ID, "baidu_translate_input")).get_attribute("value")  # 获取输入框内容
        self.driver.close()  # 关掉当前窗口
        self.driver.switch_to.window(self.driver.window_handles[-1])  # 会到原窗口

运行发现没毛病,mac、win、liunx通吃,问题解决下班回家

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值