迭代webdriver下拉列表时无法等待

时间:2016-07-21 20:23:12

标签: python selenium

我需要使用python的web驱动程序的帮助。我试图使用等待迭代下拉列表。选定的下拉项会自动更新页面,因此需要使用webdriverwait来提供页面更新时间。需要遍历10个项目的下拉列表并在每个项目之间等待。关于如何做到这一点的任何例子?

1 个答案:

答案 0 :(得分:0)

您想要做的一般模式可能是:

  1. 创建对item to select, condition to wait for after selection
  2. 对的列表
  3. 在此列表中循环
  4. 我不是一个python开发人员,所以请将下面的代码作为伪代码使用python flavor:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import Select
    
    # First item in tuple is the value of select, 
    # Second is the value to wait for after selecting the item
    verify_list = [('item1', '//xpath1'),('item2', '//xpath2'),...]
    
    wait = WebDriverWait(driver, 10)
    
    for item,condition in verify_list:
    
        # Get select itself (since page refreshes, you may need to do it after each select)
        select = Select(driver.find_element_by_xpath("xpath"))
    
        # Select the value (for example by visible text)
        select.select_by_visible_text(item)
    
        # Wait for condition. For example presence of the element
        # try/catch can be added here, if you want to run all options, 
        # even if one of them fails.
        wait.until(EC.presence_of_element_located((By.XPATH, condition))