The following code uses Selenium and xsel and is expected to extract clipboard contents after driver copied to clipboard some content from a webpage:
import unittest
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import subprocess
class Test(unittest.TestCase):
def run(self):
self.driver = webdriver.Firefox()
self.driver.get('some_uri')
self.wait = WebDriverWait(self.driver, 20)
link_elem = self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "i[data-original-title='Copy to clipboard']")))
link_elem.click()
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "i[data-original-title='Copied']")))
link = subprocess.check_output(["xsel"])
print(link)
Clipboard contents are printed, but it's not the one which was copied by the python code, but some clipboard contents from the past. How to extract clipboard contents, correctly?
The solution was to use:
link = subprocess.check_output(["xsel", "--clipboard"])
instead of
link = subprocess.check_output(["xsel"])
From xsel man:
-b, --clipboard operate on the CLIPBOARD selection.
By default xsel echoes PRIMARY selection and i needed CLIPBOARD selection. More about it here.