3

On my webpage I have a window as shown in the image below. It has a division which shows the password. When I click on the "copy" element, it copies the password onto the clipboard. However this window closes within 30 seconds and then the copied password on the clipboard will be lost.

image

My intention is to save this password, copied on clipboard, into a variable to be used in later part of my python selenium code.

I'm able to successfully click this "copy" element via the automated selenium script which then copies the password to clipboard. This is my code:

    ''' I will change time.sleep() later to appropriate WebdriverWait. This is just for test. '''
    ''' This also means that I will have to save the copied password from clipboard within 10seconds, '''
    ''' before the window closes '''
    time.sleep(20)
    copy_button = self.chrome_driver.find_element_by_xpath('//div[@class="button__container__not-show-spinner"]/div[@class="button__text"]')
    copy_button.click()

I read an article closest to my query here, which talks about importing tkinter module: Accessing text copied to clipboard by python

Is there a way I can do it using Selenium Webdriver methods in Python?

Please help.

dig_123
  • 2,240
  • 6
  • 35
  • 59

1 Answers1

2

tkinter comes in shipped by default with python3, contrary to what I was thinking, and using it is pretty simple:

My adding of last 2 code-lines to the previous code mentioned in my question, worked like charm:

''' I will change time.sleep() later to appropriate WebdriverWait. This is just for test. '''
''' This also means that I will have to save the copied password from clipboard within 10seconds, '''
''' before the window closes '''
time.sleep(20)
copy_button = self.chrome_driver.find_element_by_xpath('//div[@class="button__container__not-show-spinner"]/div[@class="button__text"]')
copy_button.click()
root = tk.Tk()
print(root.clipboard_get())

Ofcourse you have to import the tkinter for this to work, and in my case this is the import statement I used:

import tkinter as tk
dig_123
  • 2,240
  • 6
  • 35
  • 59