Performing a copy and paste with Selenium 2

后端 未结 8 1883
广开言路
广开言路 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: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
    
    /// 
    
    /// 
    
    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.

提交回复
热议问题