WebdriverWait failing even though element is present

16,025

The WebDriverWait expression syntax is not correct, it should be:

WebDriverWait(driver, 60).until(ec.presence_of_element_located((By.XPATH, qID_xpath_start+str(qIDindex)+qID_xpath_end)))

Note the tuple passed into the presence_of_element_located() method.

Note that 60 is 60 seconds here.


Besides, in order to succeed with debugging and understanding what is going on, letting it fail usually helps - remove try/except and see what kind of error is raised.

Share:
16,025

Related videos on Youtube

ExperimentsWithCode
Author by

ExperimentsWithCode

Updated on June 18, 2022

Comments

  • ExperimentsWithCode
    ExperimentsWithCode almost 2 years

    Here is my code:

    def CheckQueue(driver):
        qdone = False
        qID_xpath_start = "/html/body/div[5]/form/table/tbody[1]/tr["
        qID_xpath_end = "]/td[2]/a"
        qIDindex = 1
        while qdone == False:
            print "enter loop"
            print driver.find_element_by_xpath(qID_xpath_start+str(qIDindex)+qID_xpath_end).text #This prints
            try:
                element = WebDriverWait(driver, 6000).until(ec.presence_of_element_located(By.XPATH((qID_xpath_start+str(qIDindex)+qID_xpath_end)))) #This fails
                print "found"
            except:
                qdone= True
                print "could not be found"
    
            print driver.find_element_by_xpath(qID_xpath_start+str(qIDindex)+qID_xpath_end).text
            if qdone == False:
                print driver.find_element_by_xpath(qID_xpath_start+str(qIDindex)+qID_xpath_end).text
                print "testing"
    
            qIDindex +=1
            print "loop"
        return driver
    

    I get this returned (14453 is the link text of the xpath I am looking for)

    enter loop
    14453
    could not be found
    14453
    loop
    

    It appears that my code is able to find the link, but then when asked to check if the link is there, it fails, and activates the except statement. Why would it fail if its already been found and printed?

    Also, it fails almost immediately even though I've allotted it so much time to look.

    Any idea where I am going wrong?

    I've tried

    element = WebDriverWait(driver, 6000).until(ec.presence_of_element_located(By.XPATH((qID_xpath_start+str(qIDindex)+qID_xpath_end))))
    
    element = WebDriverWait(driver, 6000).until(EC.presence_of_element_located(By.XPATH((qID_xpath_start+str(qIDindex)+qID_xpath_end))))
    
    element = WebDriverWait(driver, 6000).until(ec.presence_of_element_located(By.XPATH, qID_xpath_start+str(qIDindex)+qID_xpath_end))
    
    element = WebDriverWait(driver, 6000).until(ec.presence_of_element_located(By.xpath, qID_xpath_start+str(qIDindex)+qID_xpath_end))
    

    I am using Python 2.7, Selenium 2.43, Firefox 23.0.3

    By the way, I threw in several print statements that may feel out of place for the sake of testing if that xpath element could be found at certain points.

    EDIT: When I remove my try statement, I get this error.

    element = WebDriverWait(driver, 6000).until(EC.presence_of_element_located(By.XPATH((qID_xpath_start+str(qIDindex)+qID_xpath_end))))
    

    NameError: global name 'By' is not defined

    I have the following import statements in my code:

    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
    from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
    from selenium.webdriver.support.ui import Select
    from selenium.webdriver.common.action_chains import ActionChains
    

    I assume I would do need to add an import statement, but I can't seem to find how I import that By.

    I tried:

    from selenium.webdriver.common.by import By
    

    When I ran it, I made it past that error but received this error:

    element = WebDriverWait(driver, 6000).until(EC.presence_of_element_located(By.XPATH((qID_xpath_start+str(qIDindex)+qID_xpath_end)))) TypeError: 'str' object is not callable

    I then updated my WebDriverWait line to reflect alecxe's suggestion.

    element = WebDriverWait(driver, 6000).until(EC.presence_of_element_located((By.XPATH, qID_xpath_start+str(qIDindex)+qID_xpath_end)))
    

    and now it seems to be working.

    • alecxe
      alecxe over 9 years
      Remove the try/except block and post the complete error traceback you get.
  • TayTay
    TayTay about 8 years
    This is late, but it's worth pointing out that WebDriverWait takes seconds, NOT millis. Leaving 6000 in this answer may end up throwing someone off.
  • alecxe
    alecxe about 8 years
    @Tgsmith61591 ah, I know how I mixed that up - webdriverjs uses milliseconds for the browser.wait()..too much different selenium bindings for me :)
  • TayTay
    TayTay about 8 years
    I do the same thing. Anytime I go from Java to Python, I inadvertently end up waiting for a long time before I realize time.sleep(1000) is not the same as Thread.sleep(1000) :-)
  • Alexei Martianov
    Alexei Martianov almost 8 years
    if you want part of xref, you can use presence_of_element_located((By.XPATH, '//a[starts-with(@href,"https://www.bla-bla")]'))). Don't know if can be written shorter, just saw it in other post.
  • alecxe
    alecxe almost 8 years
    @AlexeiMartianov it can be: presence_of_element_located((By.CSS_SELECTOR, 'a[href^="https://www.bla-bla"]'))) :)
  • Alexei Martianov
    Alexei Martianov almost 8 years
    @alecxe, where to read about such conditions? googling starts-with did not get results. thanks