Can selenium handle autocomplete?

前端 未结 17 1272
粉色の甜心
粉色の甜心 2020-12-09 04:26

I have a test case that requires typing in a partial value into an ajax based textfield and verifying the list has the expected content. If it does, select the content. An

相关标签:
17条回答
  • 2020-12-09 05:05

    I used these commands in Selenium IDE 2.9.1 Version for the autocomplete text field. sendKeys (locator,value) clickAt(locator, coordString) click(locator)

    0 讨论(0)
  • 2020-12-09 05:06

    For WebDriver, try this

    The below code is for searching a text automatically from the auto suggest; mainly for a list item.

    driver.findElement(By.id("your searchBox")).sendKeys("your partial keyword");
    Thread.sleep(3000);
    List <WebElement> listItems = driver.findElements(By.xpath("your list item locator"));
    listItems.get(0).click();
    driver.findElement(By.id("your searchButton")).click();
    
    0 讨论(0)
  • 2020-12-09 05:08

    In the help text for the typeKeys command it says:

    In some cases, you may need to use the simple "type" command to set the value of the field and then the "typeKeys" command to send the keystroke events corresponding to what you just typed.

    So use this combination of

    type(selector, text);
    typeKeys(selector, text);
    

    This seems to work well.

    0 讨论(0)
  • 2020-12-09 05:11

    I'd do this as follows:

    • type to enter the value in the text field.
    • waitForTextPresent or verifyTextPresent to check the autocomplete content
    • click or mouseDown to click on the item in the autocomplete list

    The trick is going to be making the final click be just in the right place. You should be able to use an XPath expression that searches for the text you're expecting to find it.

    0 讨论(0)
  • 2020-12-09 05:13

    This may not work for everyone, but I simply added in a method that allowed me to type in characters with a delay.

    Actions builder = new Actions(this.webDriver);
        WebElement element = this.getWebElement();
        for (char c : value.toCharArray()) {
            builder = builder.sendKeys(element, c + "");
            builder.pause(100);
        }
    
        builder.build().perform();
    

    I then found the item that I wanted to click (

    resultsElement.findElement(By.xpath("//li[.='" + valueLabel + "']"))
    

    Where container is the resultsElement is the WebElement that contains the result set and value label is the value I want to click.

    Again, it may not work for all, but it worked for me and I thought it prudent to share.

    0 讨论(0)
  • 2020-12-09 05:14

    Your question is slightly ambigious.

    Most browsers keep a value cache that is based on the name of the field: This is the value that is being suggested as autocompletion by your browser even though you may never have visited the site before. This feature is non-standard across all browsers, and there's going to be no standard way for selenium to detect/analyze this. You can still do it, but you'll have to make javascript functions that determine the values yourself. Then you can use "eval" in selenium to execute these functions. I have not seen any js libraries that can tell you these values in a cross-browser compatible way.

    The other alternative is that you use ajax to do a server-side submit of the partially entered value. In this case it's just a matter of typing the values into the textbox and asserting that the expected values turn up. Normally the autocomplete suggestions show up in some layer on the client side.

    0 讨论(0)
提交回复
热议问题