Bartłomiej 'furas' Burek
furas.pl
# prywatne notatki - Python, Linux, Machine Learning, etc.

Selenium: Send clipboad to field in browser [GB]

When you find input field on page then you can send Ctrl+V to send text from clipboard to this field.

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

driver = selenium.webdriver.Firefox()
driver.get('https://google.com')

item = driver.find_element_by_name('q')
item.send_keys(Keys.CONTROL + "v")
#item.send_keys(Keys.CONTROL, "v")

You can also try to select and copy to clipboard text but it may works only with visible text, and it can work with <body> (to get all text) but it may have problem with other elements (to get only part of text).

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

driver = webdriver.Firefox()
driver.get("http://quotes.toscrape.com")

element = driver.find_element_by_tag_name("body")

# it gets only visible text.
# it works with <body> but not have to with other tags.
element.send_keys(Keys.CONTROL, "a")
element.send_keys(Keys.CONTROL, "c")

You can also try to use pyperclip to get text from clipboard and send it as normal text.

import pyperclip

#pyperclip.copy('The text to be copied to the clipboard.')

text = pyperclip.paste()
#print(text)

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

driver = selenium.webdriver.Firefox()
driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_textarea')

frame = driver.find_element_by_id('iframeResult')
driver.switch_to.frame(frame)

item = driver.find_element_by_id('w3mission')
item.clear()
item.send_keys(text)
Książki: python-dla-kazdego-podstawy-programowania python-wprowadzenie python-leksykon-kieszonkowy python-receptury python-programuj-szybko-i-wydajnie python-projekty-do-wykorzystania black-hat-python-jezyk-python-dla-hackerow-i-pentesterow efektywny-python-59-sposobow-na-lepszy-kod tdd-w-praktyce-niezawodny-kod-w-jezyku-python aplikacje-internetowe-z-django-najlepsze-receptury