I’m facing an issue that I can’t solve for now.
I have a textbox where the user can enter a city or a post code. Once some text is entered, an API is called and returns a list of matching " " couples. Those items appear in a dropdown list and can be selected.
I would like to automate the following actions:
Enter any post code in this textbox
Select the first (or any) matching result in the dropdown list
I tried to use the Keys.chord method to use down arrow to pick up the first element in the list. Textbox is empty after the actions.
I tried the list all elements under the root textbox after entering some text in the textbox, but the returned list is empty.
How about making a list of the dropdown choices and selecting one of the items at random?
Maybe like
Random rand = new Random();
def driver = DriverFactory.getWebDriver();
List<WebElement> menuItems = driver.findElements(By.xpath('//li[@class="ui-menu-item"]'));
'get the maximum number of items In list'
int upperLimit = menuItems.size();
int choice = rand.nextInt(upperLimit);
menuItems.get(choice).click();
If you don’t want to go with a random item, then just choose the first item of the list:
with the values being the X offset (going to the right) and the Y offset (going downwards). The starting position is supposed to be the TestObject’s upper left corner.
thanks a lot for your answer!
I already tried this kind of code, but the returned list is empty
The actual behavior seems to be the following:
// I enter some postal code in the text field, so the API is called and the autocomplete propositions are displayed
WebUI.setText(findTestObject('.../text_Souscripteur_CPVille'), post_code, FailureHandling.STOP_ON_FAILURE)
// At this time, the autocomplete propoositions are empty...
List<WebElement> menuItems = driver.findElements(By.xpath('//li[@class="ui-menu-item"]'))
menuItems.get(0).click()
I don’t know why, when executing next line of code, the textbox seems to be reinitialized.
I think we are going too fast and the page needs to catch up. Try putting a wait or delay statement after the setText and before the autocomplete drop-down, like:
WebUI.setText(findTestObject('.../text_Souscripteur_CPVille'), post_code, FailureHandling.STOP_ON_FAILURE)
WebUI.sendKeys(findTestObject('.../text_Souscripteur_CPVille'), Keys.Chord(Keys.Tab))
WebUI.delay(2)
// now see if there are any items..
List<WebElement> menuItems = driver.findElements(By.xpath('//li[@class="ui-menu-item"]'))
WebUI.comment("We have " + menuItems.size().toString() + " items.")
menuItems.get(0).click()
I’ve tested your code, but the autocomplete dropdown is always empty.
While debugging, I noticed that every time I execute the “setText” code, the text field is reset when going to the next line of code… It seems that the textbox looses focus after the step is executed, but I’ve no idea why
Copy and paste the value you have for “post_code” into the textbox manually and see if the same reaction occurs. Also, do you display the contents of “post_code” for your own verification somewhere? It may be the format of the postal code needs tweaking.
If you think you might need focus on the textbox, you can add a click before your setText.
Copy and pasting the value into the textbox does display the expected dropdown list.
If I click into the textbox before entering the text, the focus seems to be kept, as the dropdown list stays displayed.
The only remaining issue is that the items list is always empty.
I tried the following codes:
Hi - should be “comment(“We have " + menuItems.size().toString() + " items.”)”. Ie specify the size of what needs to be returned (menuItems), otherwise it will always be 0. Cheers
We had a discussion on another forum question about em dash, en dash, hyphen and other dash characters looking similar but the computer sees them differently. So, how about copying the HTML of the class (double click on “ui-menu-item”, right click and select copy) and then paste this phrase (replace) in place of the contents that I typed. This just ensures we have the correct characters in the statement so our phrase matches the HTML.
Try the copy and paste of the class contents of the HTML . There may be some difference between “French” and “English” character sets on the keyboards…
And you can try to right click on the dropdown phrase, “LYON 69006” and select Inspect and see if we do have the correct information when it displays.
How about?
WebUI.click(findTestObject('.../text_Souscripteur_CPVille'))
WebUI.setText(findTestObject('.../text_Souscripteur_CPVille'), post_code, FailureHandling.STOP_ON_FAILURE)
// WebUI.sendKeys(findTestObject('.../text_Souscripteur_CPVille'), Keys.Chord(Keys.Tab))
TestObject menuItem = new TestObject()
menuItem.addProperty("xpath", ConditionType.EQUAL, 'id("ui-id-2")/li[1]')
// wait until get search result back
WebUI.waitForElementVisible(menuItem, 10)
WebUI.click(menuItem)
Wow. That’s certainly weird. The only thing I can suggest is that we are going too fast again, so maybe insert another wait or delay statement in the code. WebUI.delay(2)
How about using the alternative I suggested earlier?
I just found the cause of my issue by resolving another one: my field is located in an iframe and I never thought to add a switchToFrame() method before trying to find the dropdown web element…