How to press Ctrl+V in Selenium WebDriver - selenium

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

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();

You can try this simple way
driver.findElement(By.xpath(FileUpDownLoad._SOURCE_NAME)).sendKeys(Keys.CONTROL + "v");

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();

Related

Selenium Internet Explorer Control Click not working

I have IE 11. I am using the ie driver 3.14 32bit and writing my test in java. My os is win 10.
When I click on an element it works fine:
linkElement.click();
When I control click on the element it acts as if it is just a click:
action.keyDown(Keys.CONTROL).click(linkElement).keyUp(Keys.CONTROL).build().perform();
My goal is to open the link in a new tab - that is why I am control clicking. How do you open a link in a new tab in ie?
EDIT
Here is a simple test:
InternetExplorerOptions options = new InternetExplorerOptions();
options.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
URL driverUrl = TestTest.class.getClassLoader().getResource("IEDriverServer.exe");
String driverPath = driverUrl.getPath();
System.setProperty("webdriver.ie.driver", driverPath.toString());
InternetExplorerDriver driver = new InternetExplorerDriver(options);
driver.manage().window().maximize();
driver.get("http://www.cnn.com");
WebElement linkElement = driver.findElement(By.xpath("//*[#id=\"nav\"]/div[2]/div[2]/a[2]")); // get World link
new Actions(driver).keyDown(Keys.CONTROL).click(linkElement).keyUp(Keys.CONTROL).build().perform();
With my simplified test it opens in a new window - I have the setting in IE to open in a new tab. How can I make it open in a new tab?

SendKeys in firefox quantum using selenium

I have been trying to press (CTRL + ALT + 'f') after selecting a WebElement using selenium 3.5 on firefox quantum. This is the code I have written :
WebElement ele = m_driver.findElement(By.cssSelector(".tm-project-name"));
ele.click();
Actions act = new Actions(m_driver);
act.sendKeys(Keys.CONTROL).perform();
act.sendKeys(Keys.ALT).perform();
act.sendKeys("f").perform();
For performing this work I also tried this method
act.sendKeys(Keys.chord(Keys.CONTROL, Keys.ALT, "F")).build().perform();
Both of these methods works fine on chrome browser but fails to work in firefox quantum.
Can anyone help me out on this issue.
You can try to pass control+Alt+"f" using Robot class , this will work in all browsers.
Try the below code.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_F);
Hope this will work for you.
WebElement ele = m_driver.findElement(By.cssSelector(".tm-project-name"));
ele.send_keys(Keys.SHIFT+Keys.CONTROL+'f');
I normally write in python, and I checked it in my IDE before I submitted...Python works... thinking this is the C# version

Selenium contextClick() hangs on Edge browser

System.setProperty("webdriver.edge.driver","C:\\EdgeDriver\\" + "MicrosoftWebDriver.exe");
WebDriver driver = new EdgeDriver();
driver.navigate().to("http://www.google.com");
new Actions(driver).moveToElement(driver.findElement(By.id("lst-ib"))).contextClick().build().perform();
if (driver != null) {
driver.close();
}
When I run the above code, a context menu is popped up, but it does not proceed further from that. If I manually click on somewhere else, it proceeds further into the program.
I have checked with other browser/driver, it works without any
problem.
I am using Selenium 2.53 and MicrosoftWebDriver 14393
Any solution for this?
try System.setProperty("webdriver.edge.driver","C:\EdgeDriver\MicrosoftWebDriver.exe");
instead of
System.setProperty("webdriver.edge.driver","C:\EdgeDriver\" + "MicrosoftWebDriver.exe");

Mouse hover on element

http://www.franchising.com/ ---> Mouse over on (Franchises A-Z) ---> need to click Q
I have tried with the following
WebElement we1=driver.findElement(By.cssSelector("a[href='/franchises/']"));
WebElement we2=driver.findElement(By.cssSelector("a[href='/franchises/q.html']"));
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
((JavascriptExecutor) driver).executeScript(js, we2); // I have used the script since the we2 is not visible
Actions builder=new Actions(driver);
builder.moveToElement(we1).perform();
Thread.sleep(5000);
we2.click();
could any one try and share me the code... Still I'm getting "ElementNotVisibleException"
With firefoxdriver, a lot would depend on what version of driver you are using and what version of Firefox you have on your system since native support would differ based on that.
Following works on Chrome :
WebElement link1 = driver.findElementByLinkText("Franchises A-Z");
Actions action = new Actions(driver);
action.moveToElement(link1).click(driver.findElementByXPath("//a[contains(#href,'franchises/b')]")).perform();
Before going in to the code i just want to you to ensure the version of Selenium server you are using. Please make it to the updated version of 2.28.x
Code:
driver = new FirefoxDriver();
driver.get("http://www.franchising.com/franchises/");
Thread.sleep(5000);
WebElement element=driver.findElement(By.xpath("//tr[3]/td/table/tbody/tr/td[4]/a"));
Actions builder = new Actions(driver);
builder.moveToElement(element).build().perform();
Thread.sleep(5000);
it works fine for me. Try this code. I hope this will work.

WebDriver - Key commands in Internet Explorer

I'm using Selenium 2.28 & IE8 on WinXP32. I need to be able to send the following keyboard command to my webpage: ALT + k
The following works perfectly in Chrome & Firefox17:
- Chrome:
Actions builder = new Actions(driver);
builder.sendKeys(Keys.ALT, "k").build().perform();
- Firefox 17 (requires extra command key for same effect):
Actions builder = new Actions(driver);
builder.sendKeys(Keys.SHIFT, Keys.ALT, "k").build().perform();
I've tried all of the following in IE without success :
- builder.sendKeys(Keys.ALT, "k").build().perform();
- builder.sendKeys(Keys.ALT, Keys.SHIFT, "k").build().perform();
- builder.sendKeys(Keys.chord(Keys.ALT, "k")).build().perform();
- builder.sendKeys(Keys.chord(Keys.SHIFT, Keys.ALT, "k")).build().perform();
- builder.keyDown(Keys.ALT).sendKeys("k").build().perform();
- builder.keyDown(Keys.ALT).sendKeys("k").keyUp(Keys.ALT).build().perform();
- builder.keyDown(Keys.SHIFT).keyDown(Keys.ALT).sendKeys("k").build().perform();
- builder.keyDown(Keys.SHIFT).keyDown(Keys.ALT).sendKeys("k").keyUp(Keys.ALT).keyUp(Keys.SHIFT).build().perform();
Any suggestion on how to achieve what I want?
I've had to resort to using a Robot to do it for Internet Explorer:
import java.awt.Robot
...
Robot robot;
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_K);
robot.keyRelease(KeyEvent.VK_K);
robot.keyRelease(KeyEvent.VK_ALT);
}catch (AWTException e) {
e.printStackTrace();
}
I would prefer to do it just using Selenium APIs, but this workaround will suffice until that is possible.
The current driver (2.45) with default options uses PostMessage which is unreliable for simulating modifier keys [1] [2]. If you set the IE option "RequireWindowFocus" to true then the driver will use SendInput instead which will work.
If using C#, you would create the driver like this:
var options = new InternetExplorerOptions();
options.RequireWindowFocus = true;
var driver = new InternetExplorerDriver(options);
http://blogs.msdn.com/b/oldnewthing/archive/2005/05/30/423202.aspx
PostMessage WM_KEYDOWN send multiply keys?
To rephrase daw's answer for Java, this is the only way I've found to send keys such as Control to IE:
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);
WebDriver driver = new InternetExplorerDriver(capabilities);
This has the side effect of IEDriver taking over your mouse.