【问题标题】:python selenium send_keys waitpython selenium send_keys 等待
【发布时间】:2018-08-09 14:31:14
【问题描述】:

我对 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 谢谢你的回答

【问题讨论】:

  • 一种方法是轮询元素的text value。只要webelement没有返回python这个词,就不要点击。 (尽管在您的示例中,我相当确定在您点击之前的 time.sleep(1) 可以解决问题,但您不想使用它)
  • 您有什么证据表明在输入所有键之前发生了点击? send_keys 似乎不太可能在完成之前返回。例如,您是否尝试在单击之前获取元素的值,以查看浏览器返回的内容?会不会是您在输入元素上附加了一些导致某种延迟的 javascript?
  • 谢谢我有一个问题,因为它是针对一个元素的。如果我有一个清单怎么办。我必须等待所有元素。然后使用 send_keys 并从列表中选择一个项目?
  • @Tom1416 ,哪些元素?你到底想让你的脚本做什么?
  • 我想等待所有列表项并使用 send_keys 选择一个项目,例如。 query.send_keys ('python')

标签: python selenium selenium-webdriver wait sendkeys


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:
    #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 
    

    【讨论】:

      【解决方案3】:

      如果我正确解释了您的问题,您有一个 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()
      

      这一切都假定按钮选择了所选项目。

      【讨论】:

      • 谢谢我有一个问题,因为我可以通过 XPATH 访问我的列表,但我不知道如何选择元素,因为应用程序不点击元素
      猜你喜欢
      • 2021-08-02
      • 2018-06-18
      • 1970-01-01
      • 2018-02-04
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多