-1

I have a for loop that constantly loops back a website to fill out and submit a search box with the next string from a list.

For example, if the list contains [abcd, efgh, ijkl....], the first loop would send abcd, the second loop would send efgh and etc.

Currently the loop sends the first string correctly, but on every loop afterwards, the string sent is added to the previous string. So instead of efgh on the second loop, it is abcdefgh.

How do I clear the keys so that I can just send the individual string? Here's what I have right now:

    for(String value : List){
        driver.get(Link);
        Actions actions = new Actions(driver);
        WebElement input_field = driver.findElement(By.id("txtBoxSearch"));

        actions.moveToElement(input_field);
        actions.click();            
        actions.sendKeys(value);            
        actions.build().perform();

        WebElement submit_key = driver.findElement(By.xpath("//button[contains(@title, 'Search')]"));
        actions.moveToElement(submit_key);

        actions.click();
        actions.build().perform(); 
    }

2 Answers2

0

You need to clear the content of textbox before write value to it

for(String value : List){
    driver.get(Link);
    Actions actions = new Actions(driver);
    WebElement input_field = driver.findElement(By.id("txtBoxSearch"));
    input_field.clear();
    actions.moveToElement(input_field);
    actions.click();            
    actions.sendKeys(value);            
    actions.build().perform();
    WebElement submit_key = driver.findElement(By.xpath("//button[contains(@title, 'Search')]"));
    actions.moveToElement(submit_key);
    actions.click();
    actions.build().perform(); 
}
TuyenNTA
  • 1,194
  • 1
  • 11
  • 19
  • I have tried that before, because how the search bar is formatted i receive a Exception in thread "AWT-EventQueue-0" org.openqa.selenium.InvalidElementStateException: invalid element state: Element must be user-editable in order to clear it. – Steven Fang Jul 07 '17 at 23:18
  • @StevenFang I have edited the code, the `button` cannot be cleared. But the `textbox` must be. Please check you code again and the html for the case the id `txtBoxSearch` is duplicated, and the first element has that key is not a editable element. – TuyenNTA Jul 07 '17 at 23:28
  • It seems that the txtBoxSearch is a formControl, and thus actions such as send keys, clearing and clicking have to be .build().perform(), or else individual actions will be instantaneous and do nothing. In order to work around that, I have tried to do actions.sendKeys(Keys.CONTROL, "a", Keys.DELETE), but that only gives me a nullpointer exception. – Steven Fang Jul 07 '17 at 23:34
  • It seems the only work around for now is to perform Keys.BACKSPACE multiple times, but thats highly inefficient, and the strings i send to the search greatly vary. – Steven Fang Jul 07 '17 at 23:35
  • Before using `Ctrl + A` did you try to move to element, then click on it (to get focus)? Why the NPE throw? – TuyenNTA Jul 07 '17 at 23:37
0

Here is the Answer to your Question:

Once you call click() method on the on the desired element through action class instance, next use the action class instance to clickAndHold, send CONTROL A and then send the new text DebanjanB to the form control to overwrite the previous text as follows:

for(String value : List){
    driver.get(Link);
    Actions actions = new Actions(driver);
    WebElement input_field = driver.findElement(By.id("txtBoxSearch"));
    actions.moveToElement(input_field);
    actions.click();
    actions.clickAndHold(input_field).sendKeys(Keys.chord(Keys.CONTROL, "a"), "DebanjanB").build().perform();            
    WebElement submit_key = driver.findElement(By.xpath("//button[contains(@title, 'Search')]"));
    actions.moveToElement(submit_key);
    actions.click();
    actions.build().perform(); 
}

Let me know if this Answers your Question.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352