简体   繁体   English

python selenium send_keys等

[英]python selenium send_keys wait

I have a question regarding the send_keys function. 我有关于send_keys函数的问题。 How can I make the test wait for the entire content of send_keys to be entered? 如何让测试等待输入send_keys的全部内容? I can not use time.sleep, so I tried: 我不能使用time.sleep,所以我试过:

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 应用程序在操作完成之前单击按钮send_keys,感谢您的回答

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. 此代码应允许您等到字段中输入完整字符串。

#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 

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. 如果我正确地解释您的问题,您有一个Web控件,它提供一个“搜索”字段,该字段将根据字段的内容逐步过滤列表。 So, as you type "python", your list will get reduced to just the items that match "python". 因此,当您键入“python”时,您的列表将缩减为仅匹配“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. 这都假定该按钮选择所选项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM