可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
Pretty simple actually:
from selenium.webdriver.common.keys import Keys elem = find_element_by_name("our_element") elem.send_keys("bar") elem.send_keys(Keys.CONTROL, 'a') #highlight all in box elem.send_keys(Keys.CONTROL, 'c') #copy elem.send_keys(Keys.CONTROL, 'v') #paste
I imagine this could probably be extended to other commands as well
回答2:
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 Mac keyboard, but work because of legacy keyboard shortcuts.
Alternate keyboard shortcuts for cut, copy and paste on a Mac
- Cut => control+delete, or control+K
- Copy => control+insert
- Paste => shift+insert, or control+Y
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)
回答3:
Rather than using the actual keyboard shortcut i would make the webdriver get the text. You can do this by finding the inner text of the element.
WebElement element1 = wd.findElement(By.locatorType(locator)); String text = element1.getText();
This way your test project can actually access the text. This is beneficial for logging purposes, or maybe just to make sure the text says what you want it to say.
from here you can manipulate the element's text as one string so you have full control of what you enter into the element that you're pasting into. Now just
element2.clear(); element2.sendKeys(text);
where element2 is the element to paste the text into
回答4:
I cannot try this on OSX at the moment, but it definitely works on FF and Ubuntu:
import os import time from selenium import webdriver from selenium.webdriver.common.keys import Keys with open('test.html', 'w') as fp: fp.write("""\ """) driver = webdriver.Firefox() driver.get('file:///{}/test.html'.format(os.getcwd())) element1 = driver.find_element_by_name('intext') element2 = driver.find_element_by_name('outtext') time.sleep(1) element1.send_keys(Keys.CONTROL, 'a') time.sleep(1) element1.send_keys(Keys.CONTROL, 'c') time.sleep(1) element2.send_keys(Keys.CONTROL, 'v')
The sleep()
statements are just there to be able to see the steps, they are of course not necessary for the program to function.
The ActionChain send_key
just switches to the selected element and does a send_keys
on it.
回答5:
if you want to verify copy to clip board feature, below is the best solution
public class Main { public static void main(String[] args) { // This represents a cut-copy (Ctrl+X/Ctrl+C) operation. // The text will be copied to the clipboard // StringSelection is a Transferable implementation // class. StringSelection data = new StringSelection ("This is copied to the clipboard"); Clipboard cb = Toolkit.getDefaultToolkit() .getSystemClipboard(); cb.setContents(data, data); // This represents the paste (Ctrl+V) operation try { Transferable t = cb.getContents(null); if (t.isDataFlavorSupported(DataFlavor.stringFlavor)) System.out.println(t.getTransferData(DataFlavor .stringFlavor)); } catch (UnsupportedFlavorException | IOException ex) { System.out.println(""); } } }