简体   繁体   English

运行硒时如何在Mac上从剪贴板粘贴文本-Python

[英]How to paste text from clipboard on mac while running selenium - Python

I have a function that verifies the data copied to clipboard 我有一个功能可以验证复制到剪贴板的数据

def verify_copied_transcript_data(self):
    selector = '//input[@type="text" and @name="topic"]'
    topic_field = self.wait_for_element_by_xpath(selector)
    topic_field.clear()
    topic_field.send_keys('')
    topic_field.send_keys(Keys.COMMAND, 'v')
    topic_field_value = topic_field.get_attribute('value')
    self.assertTrue(len(topic_field_value) > 0)

I have verified that manually CMD + v does paste the copied text on the topic_field. 我已验证手动CMD + v确实将复制的文本粘贴到topic_field上。 Any idea why selenium would not simulate topic_field.send_keys(Keys.COMMAND, 'v') 知道为什么硒不能模拟topic_field.send_keys(Keys.COMMAND, 'v')

The function to copy the text is: 复制文本的功能是:

def click_copy_transcript(self):
    selector = '//div[@id="closeChatModal"]//span[contains(text(), "Copy All")]'
    self.wait_for_element_by_xpath(selector).click()

This copies the text on clipboard 这会将文本复制到剪贴板上

Try this: 尝试这个:

topic_field.send_keys(Keys.COMMAND + 'v')

The full code would be: 完整的代码为:

def verify_copied_transcript_data(self):
    selector = '//input[@type="text" and @name="topic"]'
    topic_field = self.wait_for_element_by_xpath(selector)
    topic_field.clear()
    topic_field.send_keys('')
    topic_field.send_keys(Keys.COMMAND + 'v')
    topic_field_value = topic_field.get_attribute('value')
    self.assertTrue(len(topic_field_value) > 0)

Also you can try to use ActionChains: 您也可以尝试使用ActionChains:

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

ActionChains(driver) \
    .key_down(Keys.COMMAND) \
    .key_down('v') \
    .key_up('v') \
    .key_up(Keys.COMMAND) \
    .perform()

How about this: 这个怎么样:

ActionChains(driver).key_down(u'\').key_down('v').perform()

or even: 甚至:

ActionChains(driver).key_down(u'\').send_keys('v').perform()

I've checked it on a PC using the Control key instead of Command (obviously!) and both work. 我已经在PC上使用Control键而不是Command进行了检查(很明显!),两者都可以工作。

PS. PS。 Perhaps first you might need to simulate a click into the field you want to paste the buffer. 也许首先,您可能需要模拟单击要粘贴缓冲区的字段。

As you mentioned the following code copies the text to the clipboard : 正如您提到的,以下代码将文本复制到剪贴板

def click_copy_transcript(self):
    selector = '//div[@id="closeChatModal"]//span[contains(text(), "Copy All")]'
    self.wait_for_element_by_xpath(selector).click()

Now, to copy the text from the clipboard you can use the paste() method from Pyperclip – A cross-platform clipboard module for Python as follows: 现在,要从剪贴板复制文本,可以使用Pyperclip – A cross-platform clipboard module for Pythonpaste()方法Pyperclip – A cross-platform clipboard module for Python如下所示:

import pyperclip

def click_copy_transcript(self):
    selector = '//div[@id="closeChatModal"]//span[contains(text(), "Copy All")]'
    self.wait_for_element_by_xpath(selector).click()
    topic_field.send_keys(pyperclip.paste())

Note : As per adam-p/cb.py it is mentioned as: 注意 :根据adam-p/cb.py它被称为:

Python function to copy text to clipboard (so far only supports Windows). Python函数可将文本复制到剪贴板(到目前为止仅支持Windows)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM