Skip to content
Advertisement

copy and paste in selenium python

I’m new to selenium I am trying to copy something from one page to another, the page that I copy off of already has it so if you just click on the text once it copies automatically but it is not copying anything I am not sure why

chrome_driver = '/Applications/chromedriver'
driver = webdriver.Chrome(chrome_driver)
driver.get('https://tempail.com/en/')
time.sleep(5)
driver.find_element_by_xpath('//*[@id="eposta_adres"]').click()
driver.get('https://www.instagram.com/')
driver.find_element_by_xpath('//*[@id="user_first_name"]').send_keys('Scott')
driver.find_element_by_xpath('//*[@id="user_email"]').click()
act.key_down(Keys.META).send_key("COMMAND + v").key_up(Keys.META).perform()

Advertisement

Answer

On my Linux in Firefox and Chrome works

item.send_keys(Keys.CONTROL, "v")

Like

item = driver.find_element_by_xpath('//*[@id="user_email"]')

item.send_keys(Keys.CONTROL, "v")

I tried to test it with your code but you use xpath which I can’t find on Instagam

So I tested with field Search... on current page

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
#from webdriver_manager.firefox import GeckoDriverManager
import time

driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
#driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

driver.get('https://tempail.com/en/')
time.sleep(5)
driver.find_element_by_xpath('//*[@id="eposta_adres"]').click()

driver.get('https://stackoverflow.com/questions/71543113/copy-and-paste-in-selenium-python/')
item = driver.find_element_by_xpath('//*[@name="q"]')
item.send_keys(Keys.CONTROL, "v")

BTW: I described this long time ago in

Selenium: How to send clipboad to field in browser — furas.pl.

I show also how to use module pyperclip to work with clipboard.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
#from webdriver_manager.firefox import GeckoDriverManager
import time
import pyperclip

driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
#driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

driver.get('https://stackoverflow.com/questions/71543113/copy-and-paste-in-selenium-python/')
item = driver.find_element_by_xpath('//*[@name="q"]')

#text = pyperclip.paste()  # get text from cliboard
#item.clear()
#item.send_keys(text)

pyperclip.copy("Hello World")  # put text in clipboard
item.send_keys(Keys.CONTROL, "v")
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement