Issue
I have this code below to get comments from a page. Sometimes there are no comments, hence the except code triggers. It tries the try code block for like 5-10 seconds before executing the except code. Is there any faster way to check if the elements is found or not? Preferably: If the elements is not found then execute the except code directly.
try:
comments = WebDriverWait(driver, 20).until(
EC.presence_of_all_elements_located((By.XPATH, relativeXpathToReportComments)))
# some code to be executed if the elements is found
except:
print("could not find/get comments on comment page")
Solution
In case you want to check elements existence immediately you can simply use something like this:
if driver.find_elements_by_xpath('relativeXpathToReportComments'):
# some code to be executed if the elements is found
else:
print("could not find/get comments on comment page")
driver.find_elements
returns a list of elements matching the passed locator.
In case there are such elements it returns non-empty list of found elements. Otherwise it returns an empty list recognized as False
in Python.
find_elements
and find_element
are looking to find elements up to the timeout defined by ImplicitWait
which is 0 by default.
Answered By - Prophet
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.