KeyDown(),sendKeys()在硒不工作

KeyDown(),sendKeys()在硒不工作

问题描述:

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

问:

  • 我怎样才能解决这个问题?

  • 我该如何自己调试它并发现问题?

对于调试,我把一个断点放在下面一行,然后使用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(); 

    } 
} 
+0

为什么你不使用'keysLocation.sendKeys(“shoes”)'??? –

+0

或'keysLocation.sendKeys(Keys.Shift,“shoes”)' –

keysLocation是这里的input元素,您可以使用.sendKeys()不如下使用mouseAction和它的作品: -

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

希望它会帮助你.. :)

+0

Saurabh Gaur - 这个工作,但我不明白的东西。 void sendKeys(java.lang.CharSequence ... keysToSend)是方法定义...这意味着它只能接受字符序列作为它的参数.....我怎么知道它是否也可以期待2个参数,例如sendKeys(keys.SHIFT,“鞋”)??? – Tokci

+0

@Tokci很清楚的说明它期望'java.lang.CharSequence ...'作为输入的方法,这意味着它需要一个'CharSequence'的数组,意味着像'CharSequence []',在java中''''表示' []'在args .. –

你在使用sendKeys之前,可以尝试使用clickmoveToElement功能只是移动光标到和.click()功能将确保元素已被选定

试试这个

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

没有为我工作.. – Tokci

+0

好的。我只是试过你写的这一行: 'mouseAction.keyDown(Keys.SHIFT).moveToElement(keysLocation).sendKeys(“shoes”).build().execute();''甚至'mouseAction.keyDown(Keys .SHIFT).sendKeys(“shoes”)。build()。perform();'他们在我的一个包含文本框的项目中工作。不过,我第二次提供了由@ Saurabh Gaur提供的解决方案 – Anshu

Firefox的驱动程序不响应命令KEYDOWN。

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

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