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

asked Mar 1, 2018 at 13:49
5
  • 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) Commented Mar 1, 2018 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? Commented Mar 1, 2018 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? Commented Mar 1, 2018 at 14:33
  • @Tom1416 , which elements? What exactly you want your script to do? Commented Mar 1, 2018 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') Commented Mar 1, 2018 at 14:42

3 Answers 3

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.

answered Mar 1, 2018 at 13:52
Sign up to request clarification or add additional context in comments.

Comments

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 
answered Mar 28, 2019 at 11:52

Comments

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.

answered Mar 1, 2018 at 17:14

1 Comment

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.