KeyDown(),sendKeys()不在selenium中工作

时间:2016-07-06 05:32:51

标签: java selenium selenium-webdriver automated-tests

我正在尝试使用sendKeys()将大写字母的字符串(使用KeyDown)键入amazon.in网站搜索栏,但我在搜索栏上看不到该文本。我没有看到任何错误。我使用调试模式然后我也可以找到任何错误。

问题:

  • 我该如何解决这个问题?

  • 如何自行调试并发现问题?

对于调试,我在下面的行中放置一个断点,然后使用step over选项运行每一行。 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();

  }
}

3 个答案:

答案 0 :(得分:1)

keysLocationinput元素,您可以使用.sendKeys()而不使用mouseAction,如下所示,它可以使用: -

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

希望它会对你有所帮助.. :)

答案 1 :(得分:0)

在使用click之前,您可以尝试使用sendKeysmoveToElement函数只是移动光标,.click()函数将确保元素已被选中

试试这个

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

答案 2 :(得分:0)

firefox驱动程序不响应keydown命令。

所以我的工作不是使用动作类。这是代码示例:

driver.findElement(By.name(“q”))。sendKeys(Keys.SHIFT +“big”);