簡體   English   中英

python selenium send_keys等

[英]python selenium send_keys wait

我有關於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,感謝您的回答

您可以嘗試使用以下代碼:

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

此代碼應允許您等到字段中輸入完整字符串。

#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 

如果我正確地解釋您的問題,您有一個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()

這都假定該按鈕選擇所選項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM