python selenium send_keys等

问题描述 投票:1回答:3

我有关于send_keys函数的问题。如何让测试等待输入send_keys的全部内容?我不能使用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()

应用程序在操作完成之前单击按钮send_keys,感谢您的回答

python selenium selenium-webdriver wait sendkeys
3个回答
3
投票

您可以尝试使用以下代码:

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()

此代码应允许您等到字段中输入完整字符串。


0
投票

如果我正确地解释您的问题,您有一个Web控件,它提供一个“搜索”字段,该字段将根据字段的内容逐步过滤列表。因此,当您键入“python”时,您的列表将缩减为仅匹配“python”的项目。在这种情况下,您将需要使用您的代码,但添加额外的等待列表中匹配的项目。这样的事情:

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()

这都假定该按钮选择所选项目。


0
投票
#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 
© www.soinside.com 2019 - 2024. All rights reserved.