Can selenium handle autocomplete?

前端 未结 17 1274
粉色の甜心
粉色の甜心 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:16

    A little variation using Prashanth's answer:

        /**
     * Selects the element at position idx from the autocomplete combo, considering the partialKeyword
     * @param driver
     * @param element
     * @param partialKeyword
     * @param idx
     * @throws InterruptedException
     */
    public static void selectAutoCompleteValue(WebDriver driver, WebElement element, String partialKeyword, Integer idx) throws InterruptedException{
        element.sendKeys(partialKeyword);
        Thread.sleep(1000);
        List <WebElement> listItems = driver.findElements(By.cssSelector(".ui-autocomplete-item.ui-autocomplete-list-item"));
        listItems.get(idx).click();
    }
    

    Hope this helps!

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

    Please use typeKeys instead of type. Also use mouseDown instead of click. It works fine.

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

    Patrick's answer is definitely important, I also found that focus and mouseDown is needed in the last versions of Jquery UI. I recorded a video of a test so that you can see it running in Sauce Labs: https://saucelabs.com/jobs/ad8c561be39bb7a42c9bb3a063214c95

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

    Sometime the TypeKeys Doesn't work. At this time you can use keyDown

    click the inputbox and type value and keyDown in the box.

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

    I used following sequence in IDE,

    1. typeKeys
    2. waitForTextPresent
    3. mouseOver
    4. clickAt

    and worked well

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

    The type command may not be enough to trigger the autocomplete. Dave Webb's suggestions are otherwise spot on. My only addition would be that you might need the typeKeys command, which causes slightly different JavaScript events to be fired, which may be more likely to trigger the autocomplete widget.

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