How can I send keyboard shortcut Alt + Shift + Z (hotkey) with Selenium 2? - selenium

I am trying send a shortcut with Actions.sendKeys, but it isn't working.
(New Actions(driver)).SendKeys(Keys.ALT, Keys.SHIFT, "z");

You can check this question to refer about this - Pressing Ctrl+A in Selenium WebDriver
Check the answer which uses the chord method. In your case, you can do this -
String selectAll = Keys.chord(Keys.ALT, Keys.SHIFT,"z");
driver.findElement(By.tagName("html")).sendKeys(selectAll);

This can also be done using Actions keyUp and keyDown functions.
WebDriver driver = new FirefoxDriver();
Actions keyAction = new Actions(driver);
keyAction.keyDown(Keys.ALT).keyDown(Keys.SHIFT).sendKeys("z").keyUp(Keys.ALT).keyUp(Keys.SHIFT).perform();

Try it:
SendKeys.SendWait("%+z")

Assuming you're using JavaScript,
Keys.chord(keys)
Also, the documentation is at https://www.selenium.dev/documentation/en/

Apart from the Keys.chord(Keys.ALT, Keys.SHIFT,"z"); method as suggested in the other/accepted answer, I would suggest you try the Robot framework for using keyboard shortcuts.
You can do something like;
Robot robot = new Robot();
Thread.sleep(1000);
robot.delay(3000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_Y);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_Y);
I guess this would help sort your problem.

Related

Unable to type in Text box field using sendKeys method. The xpath is working fine, since when i tried to click on textbox it works

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
WebElement External = driver.findElement(By.xpath("//input [#id ='External_Reviewer']"));
External.click();
External.sendKeys("kevin");
The textbox is an autosuggestion with a people picker field. Is there any other way than using only "sendKeys" method.
Thanks in Advance.
You can use a JavaScript executor
External =driver.findElement(By.xpath("//input [#id ='External_Reviewer']"));
driver.executeScript("arguments[0].setAttribute('value', '"yourvalue"')", External);
Selenium executes so fast. You just need to add some wait after click is performed and then use sendKeys().
So, it will be like:
External.click();
Thread.sleep(2000);
External.sendKeys("kevin");
if sendKeys() not working, you can try with Actionsclass. Please try with below function:
public void typeTextIntoSpecialInput(WebElement elem, String input) {
Actions actions = new Actions(driver);
this.moveToElement(elem, true);
actions.sendKeys(input);
actions.build().perform();
}
I hope it will help.
I had faced similar issue, and clicking on the text box before sending keys worked for me

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

I want to perform right click using webdriver in Chrome ? Please Suggest

I want to perform right click using web driver in Chrome ? Please Suggest
Below is the code I am using:-
Actions actions = new Actions(driver);
Action action=actions.contextClick(element).build();
action.perform();
This works for me in c#
Actions actions = new Actions(Driver.Instance);
actions.ContextClick(element).Perform();
Actions action= new Actions(driver);
action.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
if you want to select the first option from the right click drop down.
Try the below code:
Actions act = new Actions(driver); // where driver is WebDriver type
act.moveToElement(webElement).perform();
act.contextClick().perform();

How to check tool tip in selenium web driver?

Somebody please suggest me how can i test tool tip text in selenium web driver.
I am trying to find out but its not work:
Actions action = new Actions(driver);
WebElement web1 = driver.findElement(By.id("txtEmailId"));
action.moveToElement(web1).click().build().perform();
Use Java Robot for the UI interaction; Robot is used here for controlling Mouse actions.
WebElement targetElement = driver.findElement(By.id("txtEmailId"));
Point coordinates = targetElement.getLocation();
Robot robot = new Robot();
robot.mouseMove(coordinates.getX(), coordinates.getY() + 65); //Number 65 should vary
Thread.sleep(3000);
String tooltip = driver.findElement(By.id("txtEmailId"")).getAttribute("title");
System.out.println(tooltip);
to capture ToolTip you can first perform Mouse hover and then you can call getText() method which will capture the text in String format.
Below link will guide you for the same.
http://learn-automation.com/how-to-capture-tooltip-in-selenium-webdriver/

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.