繁体   English   中英

python selenium send_keys CONTROL,'c' 不复制实际文本

[英]python selenium send_keys CONTROL, 'c' not copying actual text

我成功地突出显示了 web 页面中的部分,但是 send_keys, .send_keys(Keys.CONTROL, "c")没有将要复制的文本放在剪贴板中,只有我手动复制的最后一件事在剪贴板中:

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**

然后我尝试使用 Firefox 编辑菜单到 select 全部并复制文本,但也没有用,除了可能提到的错误之外找不到任何在线帮助(尝试旧版本 Firefox,但没有解决问题)。 有任何想法吗?

尝试使用以下代码:

包括下面的标题以导入 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)

试试这个:

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")

这个确实有效,它更新到这个日期并且还测试了几次。

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 现在非常有用,不要忘记.perform()每个动作

要在类中使用此函数:

text_box.click() #or other clicking function so you are actually typing
self.clear_text()  # Because it stands by itself

您没有定义“elim”是什么尝试:

elim = driver.find_element_by_id("some_id")
elim.send_keys(Keys.CONTROL, "a")
elim.send_keys(Keys.CONTROL, "c")

NameError: 名称 'Keys' 未定义

这意味着您必须在 Selenium 项目中导入密钥。

from selenium.webdriver.common.keys import Keys

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM