rokpoto.com
rokpoto.com

Reputation: 10784

How to extract clipboard contents with xsel after clicking on element with Selenium

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?

Upvotes: 1

Views: 1198

Answers (1)

rokpoto.com
rokpoto.com

Reputation: 10784

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.

Upvotes: 1

Related Questions