问题
In one of my automated tests I need to press Ctrl+V in text box to paste text in it. But I can't do that. I'm using Selenium WebDriver for .net v. 2.35.0.0. Here is my code, it does not work. It presses Ctrl and then V, but text not gets pasted in the box:
IWebDriver webDriver = new InternetExplorerDriver();
webDriver.Navigate().GoToUrl(@"C:\Users\us\Documents\Visual Studio 2012\Projects\SeleniumTests\SeleniumTests\test.html");
var el = webDriver.FindElement(By.XPath(".//*[@id='fld']"));
el.Click();
Actions builder = new Actions(webDriver);
builder.KeyDown(el, Keys.LeftControl).Perform();
builder.SendKeys(el, "v").Perform();
builder.KeyUp(el, Keys.LeftControl).Perform();
webDriver.Quit();
Update: OS: Windows Server 2012, x64 Browser: IE10
回答1:
Here's what I would suggest:
IWebDriver webDriver = new InternetExplorerDriver();
webDriver.Navigate().GoToUrl(@"C:\Users\us\Documents\Visual Studio 2012\Projects\SeleniumTests\SeleniumTests\test.html");
var el = webDriver.FindElement(By.XPath(".//*[@id='fld']"));
el.Click();
el.SendKeys(Keys.CONTROL+ "v");
webDriver.Quit();
回答2:
You can try this simple way
driver.findElement(By.xpath(FileUpDownLoad._SOURCE_NAME)).sendKeys(Keys.CONTROL + "v");
回答3:
The accepted answer did not work for me, what worked for me was:
new Actions(driver).KeyDown(OpenQA.Selenium.Keys.Control).SendKeys("v").KeyUp(OpenQA.Selenium.Keys.Control).Perform();
来源:https://stackoverflow.com/questions/18322883/how-to-press-ctrlv-in-selenium-webdriver