I'm having trouble finding how to implement the implicit wait when I have a string variable in the xpath.
I'm currently using a 10 second explicit wait before getting to this snippet and it works good, but I don't want to wait 10 seconds if I don't have to (it's usually around 6 seconds to load)
try:
link = driver.find_element_by_xpath("//tr[@data-recordindex = '"+str(i)+"']//img[contains(@class,'x-tree-expander')]")
datarow = driver.find_element_by_xpath("//tr[@data-recordindex = '"+str(i)+"']")
print("Level: " +str(level)+ ": " +datarow.text)
link.click()
except NoSuchElementException:
not_a_point = False
I've tried this and it's not waiting, just taking the next link and not the one I'm waiting on to appear. I'm assuming I can't just put the same thing in By.XPATH as I put in find_element_by_xpath()
.
try:
link = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//tr[@data-recordindex = '"+str(i)+"']//img[contains(@class,'x-tree-expander')]")))
datarow = driver.find_element_by_xpath("//tr[@data-recordindex = '"+str(i)+"']")
print("Level: " +str(level)+ ": " +datarow.text)
link.click()
except NoSuchElementException:
not_a_point = False
You need to take care of a couple of things as follows:
visibility_of_element_located()
and you can use the following Locator Strategy:Using variable:
Using
%s
:Using
{}
:References
You can find a couple of relevant detailed discussions on how to deal with variables within xpath in: