python selenium send_keys CONTROL, 'c' not copying actual text
Asked Answered
F

5

5

I successfully highlight the section in a web page, but send_keys, .send_keys(Keys.CONTROL, "c"), does not place the intended text to copy in clipboard, only the last thing I manually copied is in clipboard:

from selenium import webdriver 

from selenium.webdriver.common.keys import Keys 

driver = webdriver.Firefox() 

driver.get("http://www.somesite.com") 

driver.find_element_by_id("some id").send_keys(Keys.CONTROL, "a") #this successfully highlights section I need to copy 

elem.send_keys(Keys.CONTROL, "c") # this does not actually copy text**

I tried then using the Firefox edit menu to select all and copy text, but didn't work either and cant find anything online to assist other than possible mention of a bug (tried old version of Firefox, but didn't solve issue). Any ideas?

Fiche answered 11/6, 2016 at 11:18 Comment(2)
Are you declaring elem anywhere before the elem.send_keys line? What happens if you swap this out for driver.find_element_by_id("some id").send_keys(Keys.CONTROL, "c")?Butylene
Awesome, thanks very much, your suggestion worked. I replaced "elem.send_keys(Keys.CONTROL, "c")" with "driver.find_element_by_id("ires").send_keys(Keys.CONTROL, "c")"Fiche
N
7

Try using the code below:

Include the header below to import ActionChains

from selenium.webdriver.common.action_chains import ActionChains


actions = ActionChains(driver)

actions.key_down(Keys.CONTROL)

actions.send_keys("c")

actions.key_up(Keys.CONTROL)
Namhoi answered 24/5, 2019 at 12:10 Comment(1)
For everyone looking at this answer, please note that .perform() must be called after each action such as actions.key_down(Keys.CONTROL).perform()Neilson
B
4

Try this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys 

driver = webdriver.Firefox()
driver.get("http://www.somesite.com")  
driver.find_element_by_id("some id").send_keys(Keys.CONTROL, "a")
driver.find_element_by_id("some id").send_keys(Keys.CONTROL, "c")
Bookstack answered 26/9, 2019 at 17:23 Comment(0)
K
3

This one actually works, it is updated to this date and also tested several times.

from selenium.webdriver.common.action_chains import ActionChains


def clear_text(self):
    webdriver.ActionChains(self.driver).key_down(Keys.CONTROL).perform()
    webdriver.ActionChains(self.driver).send_keys("a").perform()
    webdriver.ActionChains(self.driver).key_up(Keys.CONTROL).perform()
    webdriver.ActionChains(self.driver).send_keys(Keys.DELETE).perform()
    

ActionChains are very useful nowdays, don't forget to .perform() every action

To use this functionswhile in a class:

text_box.click() #or other clicking function so you are actually typing
self.clear_text()  # Because it stands by itself
Kalil answered 11/1, 2021 at 12:53 Comment(0)
A
1

You did not define what "elim" is try:

elim = driver.find_element_by_id("some_id")
elim.send_keys(Keys.CONTROL, "a")
elim.send_keys(Keys.CONTROL, "c")
Actinon answered 5/9, 2020 at 2:26 Comment(0)
C
0

NameError: name 'Keys' is not defined

It means you have to import Keys in your Selenium Project.

from selenium.webdriver.common.keys import Keys
Continuum answered 18/11, 2022 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.