'How to get copied clipboard text from Selenium using Python?
I have a selenium python script that clicks a button and once the button is clicked some text is copied. I couldn't get the text through the code directly, but when clicking the button I verified manually that it does copy to the clipboard. So I have to now leverage the clipboard and get the text.
Here is my script
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
CHROMEDRIVER_PATH = "C:\Program Files (x86)\Google\chromedriver.exe"
chrome_options = webdriver.ChromeOptions()
# have to copy n paste the "user data" folder to a unique one otherwise they fight over each other
chrome_options.add_argument(r'user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\Selenium Data')
chrome_options.add_argument(r'--profile-directory=Profile 2')
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging']) # ignore dumb USB errors
driver = webdriver.Chrome(options=chrome_options, service=Service(CHROMEDRIVER_PATH))
driver.get("url")
username_copy = driver.find_element(By.XPATH, '//*[@id="rc-tabs"]/div/div[1]/div[2]/div[3]/div[1]/div[2]/div')
username_copy.click()
So far I have tried the following, but nothing is returned.
driver.find_element(By.XPATH,'//*[@id="rc-tabs"]/div/div[1]/div[2]/div[3]/div[1]/div[2]/div').text
text = username_copy.click()
print(text)
Unfortunately, sending ctrl v command or any shortcut is not a solution because I want to pass this variable from python into a PowerShell script. Ideally I would like to figure out a way to accomplish this without the need to install additional python packages. All in all my goal is something along the lines of this:
username_copy = driver.find_element(By.XPATH, '//*[@id="rc-tabs"]/div/div[1]/div[2]/div[3]/div[1]/div[2]/div')
username = username_copy.click()
run_Powershell(ps_script, username)
Appreciate any help in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
