Get value of clipboard from a Selenium Grid via Python

1,583 views
Skip to first unread message

Nicola Caldognetto

unread,
Mar 12, 2023, 8:54:29 AM3/12/23
to Selenium Users

I'm using Selenium for Python to connect to my Grid and command a browser. No, I'm trying to get the data of the clipboard of my Browser of the grid and copy them into my Python code.

I've tried to use from tkinter import Tk and clipboard = Tk().clipboard_get() but it clearly copy the clipboard on my host and not the one the Selenium Grid

Is there a way to access it?

rkms_82

unread,
Mar 13, 2023, 2:29:09 PM3/13/23
to Selenium Users
Unfortunately, there is no direct way to access the clipboard of a browser running on a Selenium Grid node, as the browser is running in a separate environment from your Python code.

However, there are a couple of workarounds you can try:

1. Use JavaScript to copy the contents of the clipboard to a variable, and then retrieve the variable value using Selenium's execute_script() method. Here's an example code snippet:

# Get the clipboard contents using JavaScript
clipboard_text = driver.execute_script('return navigator.clipboard.readText()')

# Print the clipboard contents
print(clipboard_text)


Note that this method requires the browser to have the clipboard-read permission, which may not be available in all browsers or in all situations.

2. Use a third-party package like pyperclip or clipboard to interact with the clipboard on the local machine, and then send the contents to the browser using Selenium's send_keys() method. Here's an example code snippet using pyperclip:

import pyperclip

# Copy the clipboard contents to a variable using pyperclip
clipboard_text = pyperclip.paste()

# Find a text input field on the page and send the clipboard contents to it
input_field = driver.find_element_by_xpath('//input[@type="text"]')
input_field.send_keys(clipboard_text)

This method requires an input field to be present on the page where you can send the clipboard contents. Note that this method also requires the pyperclip package to be installed on your system.

I hope this helps! Let me know if you have any further questions

David

unread,
Mar 13, 2023, 5:44:04 PM3/13/23
to Selenium Users
Note that method 2 with 3rd party package may work differently in the other scenario of copying data from grid node to be pasted/extracted into test code (on local host) to validate things or preprocess data to use in subsequent test actions. In that case, the same 3rd party tool approach can work, but you'd have to run that tool/library code on the grid node as a separate server process (i.e. say another remote server type instance) and you have some logic to invoke it remotely to transfer the data to your test code in local host. It gets more complicated than the other approach described where you're using clipboard of local host and just sending it over to the grid node.


If you can, obviously, method 1 is the ideal approach for its simplicity.

Reply all
Reply to author
Forward
0 new messages