WebdriverWait error although item is present
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 am returning this (14453 is the xpath link text I am looking for)
enter loop
14453
could not be found
14453
loop
It looks like my code can find a link, but then when asked to check if there is a link, it fails and fires the except statement. Why doesn't it work if it has already been found and printed?
Also, it doesn't fire almost immediately, although I gave it so much time to look.
Any idea where I am going wrong?
I 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 a few print statements that might be out of place for testing if this xpath element can be found at certain points.EDIT . When I remove the 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 undefined
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 am guessing I need to add an import statement, but I cannot find how I am importing this.
I tried:
from selenium.webdriver.common.by import By
When I ran it I missed this error but got this error:
element = WebDriverWait (driver, 6000) .until (EC.presence_of_element_located (By.XPATH ((qID_xpath_start + str (qIDindex) + qID_xpath_end)))) TypeError: object 'str' could not be called
Then I updated the 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 works.
source to share
The expression syntax is not correct, it should be: WebDriverWait
WebDriverWait(driver, 60).until(ec.presence_of_element_located((By.XPATH, qID_xpath_start+str(qIDindex)+qID_xpath_end)))
Note that the tuple is passed to the method presence_of_element_located()
.
Note that here 60
is 60 seconds.
Also, to get good at debugging and understanding what's going on, giving it up usually helps - remove it try/except
and see what error occurs.
source to share