python Seleniu发送_键等待
我有一个关于send_keys功能的问题。如何使测试等待输入send_键的全部内容?我无法使用时间。睡眠,因此我尝试:python Seleniu发送_键等待,python,selenium,selenium-webdriver,wait,sendkeys,Python,Selenium,Selenium Webdriver,Wait,Sendkeys,我有一个关于send_keys功能的问题。如何使测试等待输入send_键的全部内容?我无法使用时间。睡眠,因此我尝试: 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') drive
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()
在操作完成发送密钥之前,应用程序会单击该按钮
感谢您的回答您可以尝试使用以下代码:
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()
此代码应允许您等待,直到在字段中输入完整字符串。如果我正确解释了您的问题,您有一个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()
这一切都假定按钮选择所选项目。一种方法是轮询元素的
文本值。只要未从webelement返回单词python
,就不要单击。(虽然在您的示例中,我相当肯定在单击之前使用time.sleep(1)
可以解决问题,但您不想使用它)您有什么证据表明在输入所有键之前单击就发生了?似乎send_key
不太可能在完成之前返回。例如,您是否尝试在单击之前获取元素的值,以查看浏览器返回的内容?可能是您在输入元素上附加了一些javascript导致了某种延迟吗?谢谢,我有一个问题,因为它是针对一个元素的。如果我有一张清单呢。我必须等待所有的元素。然后使用send_键并从列表中选择一项?@Tom1416,哪些元素?您希望脚本具体执行什么操作?例如,我希望等待所有列表项并使用send_键选择一个项。query.send_keys('python')谢谢,我遇到了一个问题,因为我可以通过XPATH访问我的列表,而且我不知道如何选择元素,因为应用程序不单击元素
#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