我对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之前单击按钮 谢谢你的回答
答案 0 :(得分:2)
您可以尝试使用以下代码:
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()
此代码应允许您等到字段中输入完整字符串。
答案 1 :(得分:0)
如果我正确解释您的问题,您有一个网页控件,提供"搜索"将根据字段的内容逐步过滤列表的字段。因此,当您键入" 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()
这一切都假设该按钮选择了所选项目。
答案 2 :(得分: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