How to press Ctrl+V in Selenium WebDriver

无人久伴 提交于 2020-04-13 06:20:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!