以下代码使用 Selenium和 xsel驱动程序将网页中的某些内容复制到剪贴板后,预计会提取剪贴板内容:
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)
打印了剪贴板内容,但不是Python代码复制的内容,而是过去的一些剪贴板内容。如何正确提取剪贴板内容?
最佳答案
解决方案是使用:
link = subprocess.check_output(["xsel", "--clipboard"])
而不是
link = subprocess.check_output(["xsel"])
来自xsel man :
-b, --clipboard operate on the CLIPBOARD selection.
默认情况下,xsel 会回显主选择,而我需要CLIPBOARD 选择。 更多关于它 here .
关于python - 使用 Selenium 单击元素后如何使用 xsel 提取剪贴板内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48359983/