Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying text using selenium send_keys in python

Trying to copy text using selenium python commands but for some reason it doesn't seem to work

Here is my code:

driver.get('https://temp-mail.org/en/') #opens the website
emailID = driver.find_element_by_xpath('//*[@id="mail"]') #find the email ID
ActionChains = ActionChains(driver)
ActionChains.double_click(emailID).perform()
ActionChains.send_keys(keys.CONTROL + 'c').perform()

Instead of:

ActionChains.send_keys(keys.CONTROL + 'c').perform()

I have also tried:

emailID.send_keys(keys.CONTROL + 'c')

But seem to constantly be getting this error:

module 'selenium.webdriver.common.keys' has no attribute 'CONTROL'

EDIT:

driver.get('https://google.com ') #opens the website
input = driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input')
ActionChains.send_keys(Keys.CONTROL + 'v').perform()

Error:

Traceback (most recent call last):
  File "C:/Users/Shadow/PycharmProjects/untitled1/venv/Test.py", line 28, in <module>
    ActionChains.send_keys(Keys.CONTROL + 'v').perform()
  File "C:\Users\Shadow\PycharmProjects\untitled1\venv\lib\site-packages\selenium\webdriver\common\action_chains.py", line 336, in send_keys
    if self._driver.w3c:
AttributeError: 'str' object has no attribute '_driver'
like image 402
Dan Avatar asked Sep 13 '26 09:09

Dan


1 Answers

Your error is occurring b'coz you have imported the module selenium.webdriver.common.keys.

You should be using the Keys class within that module.

from selenium.webdriver.common.keys import Keys

#...

ActionChains.send_keys(Keys.CONTROL + 'c').perform()

EDIT

It is actually copying the text to the clipboard. You can use a library such as pyperclip to get the text.

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import pyperclip
driver = Chrome('drivers/chromedriver')
driver.get('https://temp-mail.org/en/')
emailID = driver.find_element_by_xpath('//*[@id="mail"]') 
ActionChains = ActionChains(driver)
ActionChains.double_click(emailID).perform()
ActionChains.send_keys(Keys.CONTROL + 'c').perform()
text = pyperclip.paste()
print(text)

Output

[email protected]
like image 56
Bitto Bennichan Avatar answered Sep 15 '26 22:09

Bitto Bennichan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!