3

I am automating an application which has auto suggest text box. I am facing an issue that sometimes the auto suggest values are not shown up at all. This happens 50% of the time. Worrying part is this never happens when I try manually.

I am using latest version of Selenium WebDriver in .net C# and executing the tests on Chrome.

Here is how the steps are in the Code

  1. Create WebDriver instance
  2. Find the TextBox
  3. TextBox.Click() on the TextBox
  4. TextBox.SendKeys(): I am entering value character wise which reduces the failure rate
  5. WebDriver.wait(wait for dropdown)
  6. TextBox.SendKeys(Keys.DownArrow)
  7. TextBox.SendKeys(Keys.Enter)
  8. TextBox.SendKeys(Keys.Tab)

Any pointers will really be helpful

1
  • I suppose that the suggest text box does not disappear after some time, right? Commented Jan 17, 2014 at 3:23

6 Answers 6

2

A little hacky, but might work: You do the SendKeys(), and then wait with a WebDriverWait. I don't really know the C# syntax, but should be similar:

(new WebDriverWait(yourdriver, 10)).until(visibilityOf(theDropdownWebelement));

This would throw a TimeoutException after 10 seconds of not finding the dropdown. Having wrapped all the process in a method, you can do a try/catch where, if you catch the TimeoutException, you just reload the page and try again.

Might not be the most elegant thing ever (at least, I kind of frown upon refreshing pages) but I think it might work.

0
1

Such Intermittent faults are the hardest ones to debug.
What I would like to suggest is to look around:

  1. Run tests under Fiddler. This tool may help you to find http requests with errors. Also, it would be helpful to find the request which gets the suggestion list and reissue it several times (there is an option in context menu – reissue request.)
  2. When the failure had happened, do not close the browser window, but check if there are any JavaScript errors in the Chrome Dev tools console.
  3. Try to add Thread.Sleep before failing line. If the code will pass – that means you need to improve your current WebDriverWait expression, for instance, wait for some different element visibility

If nothing will help, and the problem is not reproducible manually, I would suggest changing the test automation code to use mouse clicks instead of SendKeys()

1

I would have small sleeps (say 100ms) between each keystrokes. This far more human like then the speed in which Selenium can enter text.

Quite often the auto suggest mechanism may be triggered by delays between keystrokes, and entering quickly via selenium may not trigger.

0

I use the following procedure to perform a similar tests:

TestBox.Click();
TextBox.Clear();
TextBox.SendKeys("Test Text");
IWebElement autoCompleteOption = driver.FindElement(By.XPath"//a[contains(text(),'Test Text')]";
WaitForElementPresent(autoCompleteOption);
autoCompleteOption.Click();

The wait step is important to ensure that WebDriver doesn't generate a NoSuchElementException.

0

I too was facing the same issue. The moment sendkey() method sent the new gmail account to be created in the desired text box, some characters used to appear automatically followed by the one I intended to send. Then i removed the thread.sleep() just before sendKey(). It started working. Please find my below code:

        driver.findElement(By.name("firstName")).sendKeys("Pasupati");
        Thread.sleep(3000);
        driver.findElement(By.name("lastName")).sendKeys("Chhetri");
        //Thread.sleep(3000);
        driver.findElement(By.id("username")).sendKeys("pasupatiChhetri");
1
  • I would suggest using wait for element method as discussed in above comment Commented Jun 25, 2019 at 17:13
0

I suffered similiar problem , I was using the following code to search on instagram.

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
import time
from selenium.webdriver.common.by import By
driver = webdriver.Firefox(executable_path='/home/blank/Downloads/geckodriver-v0.27.0-linux64/geckodriver')
driver.get('https://www.instagram.com/')
driver.find_element_by_class_name('XTCLo').clear()
driver.find_element_by_class_name('XTCLo').send_keys('Ronaldo')
drop = WebDriverWait(driver,5)
drop.until(EC.visibility_of_element_located((By.CLASS_NAME,'fuqBx')))
driver.find_element_by_class_name('XTCLo').send_keys(Keys.ENTER)

Increasing the limit from 5 to 8 worked for me .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.