How to keyPress + click with selenium - 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,

Related

Selenium java webdriver 3: moveToElement not working

Selenium java webdriver 3: moveToElement not working.
WebElement element = ...
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
Tried, adding click()
WebElement element = ...
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
Not working. The mouse is not moved.
WebElement abcd = ..........
Actions actions = new Actions(driver);
actions.moveToElement(abcd).perform();
Above code will work, but please check with the chrome.exe version and the chrome version you are using it your machine. both should be compatible to one another. Check the compatibility with below link.
https://sites.google.com/a/chromium.org/chromedriver/
Skip the build() part, perform() does it underneath anyway.
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.linkText("host"));
actions.moveToElement(element).build().perform();
This will work. first check your "find element" method is write or wrong. Please post this step as well. otherwise your code is correct.
try to find element by xpath rather than link text. it worked for me.
WebElement element = driver.findElement(By.xpath("..."));
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
Try below code
public static void mouse_movement(WebDriver driver, String xpathExpression) {
Actions act = new Actions(driver);
WebElement target = driver.findElement(By.xpath(xpathExpression));
act.moveToElement(target).build().perform();
}
if you need to click the element you could try javascript:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.xpath(xPath)));
I have solved this problem with:
const element = await driver.findElement(...)
await driver.executeScript("arguments[0].scrollIntoView(true);", element)
await driver.sleep(500);

Drag and drop not working - Selenium Webdriver

I am trying to drag an element into another element using Selenium WebDriver but it's not working.
We tried with different solutions as mentioned below :
Application was built on Anjular4
WebElement From = (driver.findElement(By.xpath("")));
WebElement To = (driver.findElement(By.xpath("//*[#id='avpContainer']"));
Actions builder = new Actions(driver);
builder.clickAndHold(From).moveToElement(To).click(To).release().build().perform();
WebElement From = (driver.findElement(By.xpath("")));
WebElement To = (driver.findElement(By.xpath("//*[#id='avpContainer']"));
Actions builder = new Actions(driver);
builder.clickAndHold(From).release(To).build().perform();
WebElement From = (driver.findElement(By.xpath("")));
WebElement To = (driver.findElement(By.xpath("//*[#id='avpContainer']"));
Actions builder = new Actions(driver);
builder.dragAndDrop(From, To).build().perform();
//Setup robot
Robot robot = new Robot();
robot.setAutoDelay(50);
//Maximized browser:
robot.keyPress(KeyEvent.VK_F11);
Thread.sleep(2000);
WebElement dragElement = d.findElement('drag element');
Actions builder = new Actions(d);
builder.dragAndDropBy(dragElement,x, y).build().perform();
Can anyone help to resolve this issue.
WebElement From = driver.findElement(By.xpath(""));
WebElement To = driver.findElement(By.xpath(""));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(From)
.moveToElement(To)
.release(To)
.build();
dragAndDrop.perform();
Drag and Drop functionality won't work, if application is built on HTML5.
So, we can make this work with the support of draganddrop.js

How to select the submenu of the flyout using selenium webdriver

I want to select the submenu of the flyout using selenium webdriver.
Also how can I simulate on hover on menu items using selenium.
Please guide or provide some tutorial for the same.
Thanks
Check the actions class, something like this you need:
WebElement element = driver.findElement(By.Id("id"));
Actions builder = new Actions(driver);
Actions hover = builder.moveToElement(element);
hover.build().perform();
String New_select_Just_Landed = "//*[#id='nav1']/a";
WebElement Ba = driver.findElement(By.xpath(New_select_Just_Landed));
WebElement subBa = driver.findElement(By.xpath("//*[#id='nav1']/section/ul/li/a"));
Actions action = new Actions(driver);
action.moveToElement(Ba).perform();
Thread.sleep(2000); System.out.println("Mouse hoover succeed");
action.moveToElement(subBa).perform();
subBa.click();
System.out.println("Click Just Landed! succeed");
Thread.sleep(5000L);

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.