1

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. Any idea why selenium would not simulate 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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
UZ_IT
  • 51
  • 1
  • 6

3 Answers3

2

How about this:

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

or even:

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

I've checked it on a PC using the Control key instead of Command (obviously!) and both work.

PS. Perhaps first you might need to simulate a click into the field you want to paste the buffer.

Alichino
  • 1,668
  • 2
  • 16
  • 25
  • Thank you so much ! wish I could upvote this a 1000 times, spent too much time trying different stuff that did not work – leas Feb 23 '22 at 18:12
0

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:

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()
Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48
0

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:

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:

Python function to copy text to clipboard (so far only supports Windows).

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352