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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With