quiteLost24
quiteLost24

Reputation: 45

Copy paste through selenium Python?

Can I control + A and then somehow get the text that was highlighted with the control + A? All in Selenium Python.

Upvotes: 2

Views: 5270

Answers (2)

Muhammad Yasir
Muhammad Yasir

Reputation: 46

If you want to store the copied text in the variable then you can use pyperclip

First You need to select and copy all text using Send_keys

send_keys(Keys.CONTROL + 'a')
send_keys(Keys.CONTROL + 'c')

Then with the help of pyperclicp you can save the copied text in variable

selected_text = pyperclip.paste()
print(selected_text)

Upvotes: 1

Prophet
Prophet

Reputation: 33361

I think something like this should work

from selenium.webdriver.common.keys import Keys

src_elem = find_element_by_name("source_element")
dist_elem = find_element_by_name("distance_element")
src_elem.send_keys(Keys.CONTROL + 'a') # select all the text
src_elem.send_keys(Keys.CONTROL + 'c') # copy it
dist_elem.send_keys(Keys.CONTROL + 'v') # paste

In case that is not enough try clicking on the element before sending keys to it, like this:

from selenium.webdriver.common.keys import Keys

src_elem = find_element_by_name("source_element")
dist_elem = find_element_by_name("distance_element")
src_elem.click()
src_elem.send_keys(Keys.CONTROL + 'a') # select all the text
src_elem.send_keys(Keys.CONTROL + 'c') # copy it
dist_elem.click()
dist_elem.send_keys(Keys.CONTROL + 'v') # paste

Upvotes: 3

Related Questions