1:通过设置剪切板实现复制和黏贴
from selenium import webdriver import win32api import win32clipboard as w import win32con import time from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys #设置剪切板内容 def setText(text): w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_UNICODETEXT,text) w.CloseClipboard() def getText(): w.OpenClipboard() data=w.GetClipboardData(win32con.CF_TEXT) w.CloseClipboard() return data #读取剪切板内容 driver=webdriver.Chrome() driver.get("https://www.baidu.com") #点击百度输入框 setText("我是剪切板内容") print(getText()) time.sleep(2) driver.find_element_by_id("kw").click() ActionChains(driver).key_down(Keys.CONTROL).send_keys("v").key_up(Keys.CONTROL).perform()
2:模拟鼠标右键
from selenium import webdriver import win32api import win32clipboard as w import win32con import time from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys #设置剪切板内容 def setText(text): w.OpenClipboard() w.EmptyClipboard() w.SetClipboardData(win32con.CF_UNICODETEXT,text) w.CloseClipboard() setText(u"我是剪切板内容") options=webdriver.ChromeOptions() options.add_argument("lang=zh_cn.GBK") driver=webdriver.Chrome(chrome_options=options) driver.get("https://www.sogou.com") #获取输入框对象 ele=driver.find_element_by_id("query") ele.click() setText(u"我是剪切板内容") ActionChains(driver).context_click(ele).perform() time.sleep(2) ActionChains(driver).send_keys(‘P‘).perform()