使用 Selenium 2 执行复制和粘贴 [英] Performing a copy and paste with Selenium 2

查看:22
本文介绍了使用 Selenium 2 执行复制和粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 Selenium 2 和 Python 绑定执行复制和粘贴?

Is there any way to perform a copy and paste using Selenium 2 and the Python bindings?

我已突出显示要复制的元素,然后执行以下操作

I've highlighted the element I want to copy and then I perform the following actions

copyActionChain.key_down(Keys.COMMAND).send_keys('C').key_up(Keys.COMMAND)

但是,突出显示的文本不会被复制.

However, the highlighted text isn't copied.

推荐答案

要在 Mac 和 PC 上执行此操作,您可以使用这些备用键盘快捷键进行剪切、复制和粘贴.请注意,其中一些在物理 Mac 键盘上不可用,但由于旧键盘快捷键而起作用.

To do this on a Mac and on PC, you can use these alternate keyboard shortcuts for cut, copy and paste. Note that some of them aren't available on a physical Mac keyboard, but work because of legacy keyboard shortcuts.

  • 剪切 =>control+delete 或 control+K
  • 复制 =>控制+插入
  • 粘贴 =>shift+insert 或 control+Y

如果这不起作用,请改用 Keys.META,它是替代命令 ⌘ 键的官方密钥

来源: https://w3c.github.io/uievents/#keyboardevent

这是一个功能齐全的示例:

Here is a fully functional example:

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

browser = webdriver.Safari(executable_path = '/usr/bin/safaridriver')
browser.get("http://www.python.org")
elem = browser.find_element_by_name("q")
elem.clear()
actions = ActionChains(browser)
actions.move_to_element(elem)
actions.click(elem) #select the element where to paste text
actions.key_down(Keys.META)
actions.send_keys('v')
actions.key_up(Keys.META)
actions.perform() 

因此,在 Selenium (Ruby) 中,选择元素中的文本,然后将其复制到剪贴板的过程大致如下.

So in Selenium (Ruby), this would be roughly something like this to select the text in an element, and then copy it to the clipboard.

# double click the element to select all it's text
element.double_click 

# copy the selected text to the clipboard using CTRL+INSERT
element.send_keys(:control, :insert)

这篇关于使用 Selenium 2 执行复制和粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆