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
3 Answers 3
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.
Comments
#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 
Comments
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.
1 Comment
Explore related questions
See similar questions with these tags.
text valueof the element. As long as the wordpythonis not returned from the webelement, do not click yet. (Although in your example I'm fairly sure that atime.sleep(1)before your click would solve the problem, but you don't want to use it)send_keyswould 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?