What's wrong with my code? instead of printing: 10,9,8,7,... it only prints: 9,9,9,9,...
When the edit button is not detected, then loop for maximum of 10 seconds.
Sorry I am new on python. Thanks.
def main():
max_sec = 10
try:
WebDriverWait(driver, 1).until(EC.element_to_be_clickable(
(By.XPATH, "//button[contains(text(),'Edit')]")))
print("detected")
except:
max_sec -= 1
print(max_sec)
if max_sec == 0:
sys.exit()
print("not detected")
main()
main()
CodePudding user response:
The problem is you are calling main in every except statement, and the beginning of the main function sets max_sec=10.
CodePudding user response:
You are recursively calling main from the except block which will reset the max_sec value to 10 every time. Try and move the initial value to the function's arguments.
def main(max_sec):
try:
WebDriverWait(driver, 1).until(EC.element_to_be_clickable(
(By.XPATH, "//button[contains(text(),'Edit')]")))
print("detected")
except:
max_sec -= 1
print(max_sec)
if max_sec == 0:
sys.exit()
print("not detected")
main(max_sec)
main(10)
CodePudding user response:
The issue for you is that you decrement the counter, but the value is not used in the recursive call.
There are many different ways to code this. One possible way is as follows:
def main(max_sec=10): #################
try:
WebDriverWait(driver, 1).until(EC.element_to_be_clickable(
(By.XPATH, "//button[contains(text(),'Edit')]")))
print("detected")
except:
max_sec -= 1
print(max_sec)
if max_sec == 0:
sys.exit()
print("not detected")
main(max_sec) #####################
main()
CodePudding user response:
just declare the max_sec globally (outsite main would work).
