WebDriver - Key commands in Internet Explorer - selenium

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.

Related

Selenium Webdriver (C#): Drag and Drop executed but nothing happens

I am unable to drag and drop the W3Schools image into the rectangle using the example code people have given before. I've tried Firefox, Chrome and IE webdrivers, nothing is happening.
Firefox version 26.0
Chrome version: 31.0.1650.63 m
Internet Explorer version: 11
FirefoxDriver browser = new FirefoxDriver(); //Open up a Firefox browser
browser.Url = "http://www.w3schools.com/html/html5_draganddrop.asp";
System.Threading.Thread.Sleep(5000); // Wait so I can see what's happening
IWebElement imageToDragAndDrop = browser.FindElementById("drag1");
IWebElement boxToDragImageInto = browser.FindElementById("div2");
Actions actions = new Actions(browser);
// Attempt 1: Calling the DragAndDrop method did not work
// actions.DragAndDrop(imageToDragAndDrop, boxToDragImageInto).Build().Perform();
// Attempt 2: Calling the DragAndDrop method again but with the method calls on separate lines
//actions.DragAndDrop(imageToDragAndDrop, boxToDragImageInto);
//actions.Build();
//actions.Perform();
// Attempt 3: Calling the ClickAndHold, MoveToElement, Release methods did not work either
actions.ClickAndHold(imageToDragAndDrop);
actions.MoveToElement(boxToDragImageInto);
actions.Release(imageToDragAndDrop);
actions.Build();
actions.Perform();
HTML5 drag and drop is a known issue for Selenium;
https://code.google.com/p/selenium/issues/detail?id=6315&thanks=6315&ts=1380031813
Try using javascript directly as explained here;
HTML5 Drag and Drop using Selenium Webdriver for Ruby

KeyPress Enter for Selenium

We are doing automation testing and came around with a situation where i need to download the file from the browser .
In Download when the download button is hit we are coming to the system pop for the download where we need to perform the enter operation .
Can some one help us how to perform the enter or keyboard operation currently we are using robot API but it is not working on grid system ,
Here is my code for robot can it be enhanced and used or do we have any alternate way to do it
******** Code *************
public void downloadReportFromMyExport(WebDriver driver, String downloadSufixId) throws AWTException,
InterruptedException
{
String downloadPrefixId = ConfigProperty.getConfig("downloadPrefixId").trim();
String[] suffix;
suffix = StringUtil.split(downloadSufixId, "TR_EXP_");
String suffixPart = suffix[0];
String completeId = downloadPrefixId.concat(suffixPart);
By id = By.id(completeId);
WebElement element = driver.findElement(id);
element.click();
Robot pressKey = new Robot();
pressKey.keyPress(KeyEvent.VK_ENTER);
pressKey.keyRelease(KeyEvent.VK_ENTER);
threadSleep("5");
pressKey.keyPress(KeyEvent.VK_ALT);
pressKey.keyPress(KeyEvent.VK_F4);
pressKey.keyRelease(KeyEvent.VK_F4);
pressKey.keyRelease(KeyEvent.VK_ALT);
logger.info("Downlaod Complete");
}
In firefox browser,
Solution-1
You can change the browser settings so that it saves all downloads to that location without asking.
Refer below link to know to change that setting in firefox.
https://support.mozilla.org/en-US/kb/startup-home-page-download-settings
Solution-2
By using firefox profile setting you can achieve this.
FirefoxProfile profile=new FirefoxProfile();
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.dir","C:\\Users\\Downloads\\"); profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver=new FirefoxDriver(dc);
yeah i've encountered the same issue
better change the browser settings to save in a particular path
for handling different browsers like,
in FF,
i've used
in firefox by default the control will be on "OPEN" option so..
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
for IE (to save alt+s,to open alt+O) here im saving the file
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
for chrome
by default,when ever you click dowload button it will save without showing any popups
and i've succeeded hope it helps you
-Ajay

How to press Ctrl+V in Selenium WebDriver

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

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.

How to keyPress + click with selenium

I have a problem with my Selenium code not performing correctly the keyPress + click operation.
The test should open the jqueryui.com link and select the first 2 li elements on the page.
I am using Selenium 2.23 and Firefox 10. My code is as follows (I have trie 4 different ways to get it working but none performed):
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver browser = new FirefoxDriver(profile);
browser.get("http://jqueryui.com/demos/selectable/");
List elements = browser.findElements(By.cssSelector("ol#selectable li"));
Actions a = new Actions(browser);
a.keyDown(Keys.CONTROL)
.moveToElement(elements.get(0))
.click()
.moveToElement(elements.get(1))
.click()
.keyUp(Keys.CONTROL)
.build()
.perform();
Keyboard keyboard = ((HasInputDevices) browser).getKeyboard();
keyboard.pressKey(Keys.CONTROL);
List<WebElement> selectOptions = browser.findElements(By.cssSelector("ol#selectable li"));
selectOptions.get(1).click();
selectOptions.get(3).click();
keyboard.releaseKey(Keys.CONTROL);
Actions builder = new Actions(browser);
builder.keyDown(elements.get(0), Keys.CONTROL)
.click(elements.get(0))
.click(elements.get(1))
.keyUp(Keys.CONTROL);
Action selectMultiple = builder.build();
selectMultiple.perform();
Robot robot = new Robot();
robot.delay(1000);
robot.keyPress(KeyEvent.CTRL_MASK);
elements.get(0).click();
elements.get(1).click();
robot.keyRelease(KeyEvent.CTRL_MASK);
browser.quit();
Can anyone help me with some other suggestions?
This is a bug in Selenium, affecting shift/control/alt combined with clicks on Firefox for Windows. Star the bug and perhaps they will fix it.
I really have no idea why none of your attempts work (particularly the first one). The key constants are a mess.
Anyway, I have been able to make this work (on Windows XP):
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
elements.get(0).click();
elements.get(1).click();
robot.keyRelease(KeyEvent.VK_CONTROL);
I think that`s not a bug.
Try use this (C#):
Action builder = new Actions(driver);
builder.KeyDown(Keys.Control);
builder.Click(element1);
builder.Click(element2);
builder.KeyUp(Keys.Control);
builder.Perform();
or for you(Java):
Actions a = new Actions(browser); a.keyDown(Keys.CONTROL)
.moveToElement(elements.get(0)) .click()
.moveToElement(elements.get(1)) .click() .keyUp(Keys.CONTROL)
.build() .perform();
Just instead of
.Click(); .build(); .perform();
use
a.Click(YourWebElement);
a.keyUp(Keys.CONTROL);
a.build();
a.perform();
Should works,