本文介绍了使用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.

  • 剪切=> Ctrl + Delete或Ctrl + K
  • 复制=> control + insert
  • 粘贴=> Shift +插入或Control + Y

如果这不起作用,请改用Keys.META,它是代替命令⌘key的正式密钥

来源: 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执行复制和粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:12