Search code examples
pythonseleniumselenium-webdrivercopy-pastesendkeys

Copy text in python and paste by Ctrl + V in webdriver


I have one site where I can't enter a password using send_keys , I have to use Ctrl+V there. But I don't know how this can be done, because I have Passwords, where all passwords are stored and I need to take it from there

from Passwords import password

#how to copy?

inputpassword = driver.find_element(By.XPATH, "Path")
inputpassword.???

#how to paste by Ctrl + V

Solution

  • There are 2 steps here:

    1. Read the text from some file into the clipboard.
    2. Paste from the clipboard into the web element.
      There are several ways you can read a text from file into the clipboard, for example
    import pyperclip
    pyperclip.copy('The text to be copied to the clipboard.')
    

    More examples and explanations can be find here and here.
    Then you can simply paste the clipboard content into a web element input with Selenium with regular CTRL + V keys

    //now paste your content from clipboard
    el = self.driver.find_element_by_xpath(xpath)
    el.send_keys(Keys.CONTROL + 'v')
    

    More examples and explanations can be found here or here