6

I have a question regarding the send_keys function. How can I make the test wait for the entire content of send_keys to be entered? I can not use time.sleep, so I tried:

WebDriverWait(self.browser, 5).until(
            expected_conditions.presence_of_element_located((By.ID, "name")))
query = driver.find_element_by_id('name') 
query.send_keys('python')
driver.find_element_by_id("button").click()

the app clicks the button before the action completes send_keys thank you for an answer

Tom1416
  • 85
  • 1
  • 2
  • 11
  • One way could be to poll for the `text value` of the element. As long as the word `python` is not returned from the webelement, do not click yet. (Although in your example I'm fairly sure that a `time.sleep(1)` before your click would solve the problem, but you don't want to use it) – Chuk Ultima Mar 01 '18 at 13:52
  • What evidence do you have that the click is happening before all of the keys have been entered? It seems unlikely that `send_keys` would return before it finished. For example, have you tried getting the value of the element right before clicking, to see what the browser returns? Could it be that you have some javascript attached to the input element that is causing some sort of delay? – Bryan Oakley Mar 01 '18 at 14:18
  • 1
    Thank you I have a question, bBecause it is for one element. What if I have a list. I have to wait for all the elements. Then use send_keys and select an item from the list? – Tom1416 Mar 01 '18 at 14:33
  • @Tom1416 , which elements? What exactly you want your script to do? – Andersson Mar 01 '18 at 14:35
  • 1
    I would like to wait for all list items and using send_keys select an item, for example. query.send_keys ( 'python') – Tom1416 Mar 01 '18 at 14:42

3 Answers3

6

You could try to use the following code:

query = WebDriverWait(self.browser, 5).until(
            expected_conditions.presence_of_element_located((By.ID, "name")))
query.send_keys('python')
WebDriverWait(self.browser, 5).until(lambda browser: query.get_attribute('value') == 'python')
self.browser.find_element_by_id("button").click()

This code should allow you to wait until a full string is entered in the field.

Andersson
  • 51,635
  • 17
  • 77
  • 129
1
#to use send_keys
from selenium.webdriver.common.keys import Keys     

#enter a url inside quotes or any other value to send
url = ''
#initialize the input field as variable 'textField'                     
textField = driver.find_element_by........("")
#time to wait       
n = 10
#equivalent of do while loop in python                          
while (True):   #infinite loop                  
    print("in while loop")
    #clear the input field
    textField.clear()                   
    textField.send_keys(url)
    #enter the value
    driver.implicitly_wait(n)
    #get the text from input field after send_keys
    typed = textField.get_attribute("value")    
    #check whether the send_keys value and text in input field are same, if same quit the loop  
    if(typed == url):                   
      print(n)
      break
    #if not same, continue the loop with increased waiting time
    n = n+5 
Rajarajan
  • 21
  • 2
0

If I am interpreting your question correctly, you have a web control that provides a "search" field which will progressively filter a list based on the content of the field. So, as you type "python", your list will get reduced to just the items that match "python". in this case you'll want to use your code, but add an additional wait for the item in the list that matches. something like this:

WebDriverWait(self.browser, 5).until(
            expected_conditions.presence_of_element_located((By.ID, "name")))
query = driver.find_element_by_id('name') 
query.send_keys('python')
options_list = some_code_to_find_your_options_list
target_option = WebDriverWait(options_list, 5).until(expected_conditions.presense_of_element_located((By.XPATH, "[text()[contains(.,'python')]]")))
driver.find_element_by_id("button").click()

This all assumes that the button selects the chosen item.

Breaks Software
  • 1,721
  • 1
  • 10
  • 15
  • 1
    Thank you I have a problem because i have access to my list by XPATH and I don't know how choose element , because app don't click for element – Tom1416 Mar 02 '18 at 14:36