Tokci
Tokci

Reputation: 1280

KeyDown() ,sendKeys() not working in selenium

I am trying to type a string in capital letters (using KeyDown) into amazon.in website search bar using sendKeys() but I don't see the text on the search bar. I don't see any error. I use debug mode then I also I can find any error.

Question:

For debug I put a breakpoint on below line and then use step over option to run each line. mouseAction.moveToElement(elementLocation).build().perform();

public class MouseActions {

  public static void main (String [] args){
    System.setProperty ("webdriver.chrome.driver","C:\\Users\\tokci\\Documents\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    driver.get("http://www.amazon.in/");

    Actions mouseAction = new Actions(driver);

    //this mouse action works
    WebElement elementLocation = driver.findElement(By.xpath("//a[@id='nav-link-yourAccount']"));
    mouseAction.moveToElement(elementLocation).build().perform();

            //below code does not work
    WebElement keysLocation = driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']"));       
    mouseAction.keyDown(Keys.SHIFT).moveToElement(keysLocation).sendKeys("shoes").build().perform();

  }
}

Upvotes: 0

Views: 2217

Answers (3)

fghdfgh
fghdfgh

Reputation: 1

firefox driver does not respond to keydown command.

so my work around was not use actions class. Here's example of code:

driver.findElement(By.name("q")).sendKeys(Keys.SHIFT + "big");

Upvotes: 0

Anshu
Anshu

Reputation: 21

You can try using click before you use sendKeys. moveToElement function just moves the cursor over and .click() function will ensure that the element has been selected

Try this

WebElement keysLocation = driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']"));       
mouseAction.keyDown(Keys.SHIFT).moveToElement(keysLocation).click().sendKeys("shoes").build().perform();

Upvotes: 0

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

keysLocation is a input element here you can use .sendKeys() without using mouseAction as below and it works :-

keysLocation.sendKeys(Keys.SHIFT, "shoes");

Hope it will help you..:)

Upvotes: 1

Related Questions