Selenium Google Places Autocomplete Java -
i trying automate google auto suggestion , selecting random suggestion using selenium.
webelement element = driver.findelement(by.xpath("//input[@id='id_address']")); element.sendkeys(“whi”);
how select random suggestion list of google suggestions ?
first need find matching elements represent auto-complete suggestion options. since appearance of auto complete suggestions asynchronous, need wait them appear using loop or webdriverwait
. line gets list<webelement>
list keep trying find elements match given selector , return when list (from driver.findelements
call wraps) not empty. if doesn't find non-empty list in given timeout (10
seconds in case webdriverwait
constructor) throw timeoutexception
. then, once have list of suggestions, it's simple matter of selecting random 1 list , clicking on it.
driver.get("https://www.google.com"); driver.findelement(by.name("q")) .sendkeys("whi"); list<webelement> options = new webdriverwait(driver, 10).until(expectedconditions.numberofelementstobemorethan(by.cssselector("[role='option']"), 0)); int index = (int) (math.random() * options.size()); options.get(index) .click();
Comments
Post a Comment