Home > OS >  Copy text in python and paste by Ctrl V in webdriver
Copy text in python and paste by Ctrl V in webdriver

Time:08-29

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

CodePudding user response:

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

CodePudding user response:

I'm not sure the type of element you are getting from the source, so this may not apply. It generally works, though.

  • if you want the value in the element

inputpassword = driver.find_element(By.XPATH, "Path")
value = inputpassword.get_attribute('value')

  • if you want to set the value in the element

inputpassword = driver.find_element(By.XPATH, "Path")
inputpassword.send_keys('your value')

CodePudding user response:

I know you already accepted an answer but there's no reason to put anything into the clipboard if you are reading the password from a file.

password = ... #read password string from file
inputpassword = driver.find_element(...)
inputpassword.send_keys(password)

or just simply

password = ... #read password string from file
driver.find_element(...).send_keys(password)

Pasting in the text offers no advantages only more code which means more places where things might go wrong.

  • Related