How can I get Selenium Web Driver to wait for an element to be accessible, not just present?

后端 未结 4 1231
不思量自难忘°
不思量自难忘° 2020-11-29 23:06

I am writing tests for a web application. Some commands pull up dialog boxes that have controls that are visible, but not available for a few moments. (They are greyed out,

4条回答
  •  甜味超标
    2020-11-30 00:01

    I assume the events timeline goes like this:

    1. there are no needed elements on page.
    2. needed element appears, but is disabled:
    3. needed element becomes enabled:

    Currently you are searching for element by id, and you find one on step 2, which is earlier than you need. What you need to do, is to search it by xpath:

    //input[@id="createFolderCreateBtn" and not(@disabled)]
    

    Here's the difference:

    from lxml import etree
    
    
    html = """
    
    
    """
    
    tree = etree.fromstring(html, parser=etree.HTMLParser())
    
    tree.xpath('//input[@id="createFolderCreateBtn"]')
    # returns both elements:
    # [, ]
    
    
    tree.xpath('//input[@id="createFolderCreateBtn" and not(@disabled)]')
    # returns single element:
    # []
    

    To wrap it up, here's your fixed code:

    try:
        print "about to look for element"
        element_xpath = '//input[@id="createFolderCreateBtn" and not(@disabled)]'
        element = WebDriverWait(driver, 10).until(
                lambda driver : driver.find_element_by_xpath(element_xpath)
        )
        print "still looking?"
    finally: 
        print 'yowp'
    

    UPDATE:
    Repasting the same with the actual webdriver.
    Here's the example.html page code:

    
    
    

    Here's the ipython session:

    In [1]: from selenium.webdriver import Firefox
    
    In [2]: browser = Firefox()
    
    In [3]: browser.get('file:///tmp/example.html')
    
    In [4]: browser.find_elements_by_xpath('//input[@id="createFolderCreateBtn"]')
    Out[4]: 
    [,
     ]
    
    In [5]: browser.find_elements_by_xpath('//input[@id="createFolderCreateBtn" and not(@disabled)]')
    Out[5]: 
    []
    

    UPDATE 2:

    It works with this as well:

    
    

提交回复
热议问题