Performing a copy and paste with Selenium 2

后端 未结 8 1844
广开言路
广开言路 2020-11-30 05:05

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

相关标签:
8条回答
  • 2020-11-30 05:14

    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.

    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

    If this doesn't work, use Keys.META instead, which is the official key that replaces the command ⌘ key

    source: 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() 
    

    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)
    
    0 讨论(0)
  • 2020-11-30 05:14

    The solutions involving sending keys do not work in headless mode. This is because the clipboard is a feature of the host OS and that is not available when running headless.

    However all is not lost because you can simulate a paste event in JavaScript and run it in the page with execute_script.

    const text = 'pasted text';
    const dataTransfer = new DataTransfer();
    dataTransfer.setData('text', text);
    const event = new ClipboardEvent('paste', {
      clipboardData: dataTransfer,
      bubbles: true
    });
    const element = document.querySelector('input');
    element.dispatchEvent(event)
    
    0 讨论(0)
  • 2020-11-30 05:18

    If you want to copy a cell text from the table and paste in search box,

    Actions Class : For handling keyboard and mouse events selenium provided Actions Class

    ///

    /// This Function is used to double click and select a cell text , then its used ctrl+c
    
    /// then click on search box then ctrl+v also verify
    
    /// </summary>
    
    /// <param name="text"></param>
    
    public void SelectAndCopyPasteCellText(string text)
    
    {
    
      var cellText = driver.FindElement(By.Id("CellTextID"));
    
      if (cellText!= null)
    
      {
    
        Actions action = new Actions(driver);
    
        action.MoveToElement(cellText).DoubleClick().Perform(); // used for Double click and select the text
    
        action = new Actions(driver);
    
        action.KeyDown(Keys.Control);
    
        action.SendKeys("c");
    
        action.KeyUp(Keys.Control);
    
        action.Build().Perform(); // copy is performed
    
        var searchBox = driver.FindElement(By.Id("SearchBoxID"));
    
        searchBox.Click(); // clicked on search box
    
        action = new Actions(driver);
    
        action.KeyDown(Keys.Control);
    
        action.SendKeys("v");
    
        action.KeyUp(Keys.Control);
    
        action.Build().Perform(); // paste is performed
    
        var value = searchBox.GetAttribute("value"); // fetch the value search box
    
        Assert.AreEqual(text, value, "Selection and copy paste is not working");
    
      }
    
    }
    

    KeyDown(): This method simulates a keyboard action when a specific keyboard key needs to press.

    KeyUp(): The keyboard key which presses using the KeyDown() method, doesn’t get released automatically, so keyUp() method is used to release the key explicitly.

    SendKeys(): This method sends a series of keystrokes to a given web element.

    0 讨论(0)
  • 2020-11-30 05:28

    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

    0 讨论(0)
  • 2020-11-30 05:31

    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

    0 讨论(0)
  • 2020-11-30 05:32
    elem.send_keys(Keys.SHIFT, Keys.INSERT)
    

    It works FINE on macOS Catalina when you try to paste something.

    0 讨论(0)
提交回复
热议问题