Python: Code not running during an exception within a loopIs there a way to run Python on Android?How do I check what version of Python is running my script?Proper way to declare custom exceptions in modern Python?Manually raising (throwing) an exception in PythonWhy does Python code run faster in a function?python selenium webscraping “NoSuchElementException” not recognizedGetting InavlidOperationException error while running selenium codeSelenium ChromeDriver not going to url using default user dataStaleElementReferenceException error using Selenium in python 2.7Error with using chrome headless browser for python selenium based test cases on CentOS
Why is it 出差去 and not 去出差?
sudo passwd username keeps asking for the current password
Does there exist a non-trivial group that is both perfect and complete?
Why do you need to heat the pan before heating the olive oil?
Am I legally required to provide a (GPL licensed) source code even after a project is abandoned?
Is there a polite way to ask about one's ethnicity?
Is Newton's third law really correct?
In Street Fighter, what does the M stand for in M Bison?
In the US, can a former president run again?
Why one uses 了 and the other one doesn’t?
How to modify a string without altering its text properties
How to sort human readable size
Boundaries and Buddhism
Synaptic Static - when to roll the d6?
Large-n limit of the distribution of the normalized sum of Cauchy random variables
Is declining an undergraduate award which causes me discomfort appropriate?
I just entered the USA without passport control at Atlanta airport
What kind of chart is this?
How to make all magic-casting innate, but still rare?
How is linear momentum conserved in circular motion?
Why does a Force divides equally on a Multiple Support/Legs?
Help with coding a matrix
Why is it easier to balance a non-moving bike standing up than sitting down?
How would one carboxylate CBG into its acid form, CBGA?
Python: Code not running during an exception within a loop
Is there a way to run Python on Android?How do I check what version of Python is running my script?Proper way to declare custom exceptions in modern Python?Manually raising (throwing) an exception in PythonWhy does Python code run faster in a function?python selenium webscraping “NoSuchElementException” not recognizedGetting InavlidOperationException error while running selenium codeSelenium ChromeDriver not going to url using default user dataStaleElementReferenceException error using Selenium in python 2.7Error with using chrome headless browser for python selenium based test cases on CentOS
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am running a python code with a for loop iteration within a for loop, the code is working however, if an exception is thrown the code to execute under exception is not executing and the code loops inifinitely within the except without moving to the main loop
Error Message below:
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=73.0.3683.86)
(Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17763 x86_64)
Code that I tried:
for _ in range(100):
print("main loop pass")
for button in fb_buttons:
driver.switch_to.window(driver.window_handles[1])
try:
while like_right:
for right in like_right:
right.click()
break
driver.switch_to.window(driver.window_handles[0])
except (NoSuchWindowException, ElementNotVisibleException, StaleElementReferenceException) as e:
driver.switch_to.window(driver.window_handles[0])
continue
except StaleElementReferenceException as e:
time.sleep(10)
refresh.click()
else:
time.sleep(5)
refresh.click()
print("refreshed")
Googling/documentation came up with nothing...and it strikes me as strange that selenium is fine throwing an exception but can't catch it.
python selenium selenium-webdriver
add a comment |
I am running a python code with a for loop iteration within a for loop, the code is working however, if an exception is thrown the code to execute under exception is not executing and the code loops inifinitely within the except without moving to the main loop
Error Message below:
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=73.0.3683.86)
(Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17763 x86_64)
Code that I tried:
for _ in range(100):
print("main loop pass")
for button in fb_buttons:
driver.switch_to.window(driver.window_handles[1])
try:
while like_right:
for right in like_right:
right.click()
break
driver.switch_to.window(driver.window_handles[0])
except (NoSuchWindowException, ElementNotVisibleException, StaleElementReferenceException) as e:
driver.switch_to.window(driver.window_handles[0])
continue
except StaleElementReferenceException as e:
time.sleep(10)
refresh.click()
else:
time.sleep(5)
refresh.click()
print("refreshed")
Googling/documentation came up with nothing...and it strikes me as strange that selenium is fine throwing an exception but can't catch it.
python selenium selenium-webdriver
Your description of what is happening is very confusing. Also, it seems like trouble that you're calling the same line of code in your first except block that likely caused the exception in the first place. I'm not sure what's actually happening, but one thing I wonder is if where you have "continue", maybe you mean "break"? - the continue could very well explain why you're not "moving to the main loop".
– Steve
Mar 25 at 6:11
@Steve, I have tried with adding break in the exception, still the code under exception is not executing, basically I am trying to execute a code if exception occurs:
– aman
Mar 25 at 6:19
Here is the code that I changed:
– aman
Mar 25 at 6:20
except (NoSuchWindowException, ElementNotVisibleException, StaleElementReferenceException) as e: driver.switch_to.window(driver.window_handles[0]) refresh.click() break
– aman
Mar 25 at 6:21
add a comment |
I am running a python code with a for loop iteration within a for loop, the code is working however, if an exception is thrown the code to execute under exception is not executing and the code loops inifinitely within the except without moving to the main loop
Error Message below:
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=73.0.3683.86)
(Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17763 x86_64)
Code that I tried:
for _ in range(100):
print("main loop pass")
for button in fb_buttons:
driver.switch_to.window(driver.window_handles[1])
try:
while like_right:
for right in like_right:
right.click()
break
driver.switch_to.window(driver.window_handles[0])
except (NoSuchWindowException, ElementNotVisibleException, StaleElementReferenceException) as e:
driver.switch_to.window(driver.window_handles[0])
continue
except StaleElementReferenceException as e:
time.sleep(10)
refresh.click()
else:
time.sleep(5)
refresh.click()
print("refreshed")
Googling/documentation came up with nothing...and it strikes me as strange that selenium is fine throwing an exception but can't catch it.
python selenium selenium-webdriver
I am running a python code with a for loop iteration within a for loop, the code is working however, if an exception is thrown the code to execute under exception is not executing and the code loops inifinitely within the except without moving to the main loop
Error Message below:
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
(Session info: chrome=73.0.3683.86)
(Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17763 x86_64)
Code that I tried:
for _ in range(100):
print("main loop pass")
for button in fb_buttons:
driver.switch_to.window(driver.window_handles[1])
try:
while like_right:
for right in like_right:
right.click()
break
driver.switch_to.window(driver.window_handles[0])
except (NoSuchWindowException, ElementNotVisibleException, StaleElementReferenceException) as e:
driver.switch_to.window(driver.window_handles[0])
continue
except StaleElementReferenceException as e:
time.sleep(10)
refresh.click()
else:
time.sleep(5)
refresh.click()
print("refreshed")
Googling/documentation came up with nothing...and it strikes me as strange that selenium is fine throwing an exception but can't catch it.
python selenium selenium-webdriver
python selenium selenium-webdriver
edited Mar 25 at 9:54
Dany
2,4701821
2,4701821
asked Mar 25 at 6:00
amanaman
237
237
Your description of what is happening is very confusing. Also, it seems like trouble that you're calling the same line of code in your first except block that likely caused the exception in the first place. I'm not sure what's actually happening, but one thing I wonder is if where you have "continue", maybe you mean "break"? - the continue could very well explain why you're not "moving to the main loop".
– Steve
Mar 25 at 6:11
@Steve, I have tried with adding break in the exception, still the code under exception is not executing, basically I am trying to execute a code if exception occurs:
– aman
Mar 25 at 6:19
Here is the code that I changed:
– aman
Mar 25 at 6:20
except (NoSuchWindowException, ElementNotVisibleException, StaleElementReferenceException) as e: driver.switch_to.window(driver.window_handles[0]) refresh.click() break
– aman
Mar 25 at 6:21
add a comment |
Your description of what is happening is very confusing. Also, it seems like trouble that you're calling the same line of code in your first except block that likely caused the exception in the first place. I'm not sure what's actually happening, but one thing I wonder is if where you have "continue", maybe you mean "break"? - the continue could very well explain why you're not "moving to the main loop".
– Steve
Mar 25 at 6:11
@Steve, I have tried with adding break in the exception, still the code under exception is not executing, basically I am trying to execute a code if exception occurs:
– aman
Mar 25 at 6:19
Here is the code that I changed:
– aman
Mar 25 at 6:20
except (NoSuchWindowException, ElementNotVisibleException, StaleElementReferenceException) as e: driver.switch_to.window(driver.window_handles[0]) refresh.click() break
– aman
Mar 25 at 6:21
Your description of what is happening is very confusing. Also, it seems like trouble that you're calling the same line of code in your first except block that likely caused the exception in the first place. I'm not sure what's actually happening, but one thing I wonder is if where you have "continue", maybe you mean "break"? - the continue could very well explain why you're not "moving to the main loop".
– Steve
Mar 25 at 6:11
Your description of what is happening is very confusing. Also, it seems like trouble that you're calling the same line of code in your first except block that likely caused the exception in the first place. I'm not sure what's actually happening, but one thing I wonder is if where you have "continue", maybe you mean "break"? - the continue could very well explain why you're not "moving to the main loop".
– Steve
Mar 25 at 6:11
@Steve, I have tried with adding break in the exception, still the code under exception is not executing, basically I am trying to execute a code if exception occurs:
– aman
Mar 25 at 6:19
@Steve, I have tried with adding break in the exception, still the code under exception is not executing, basically I am trying to execute a code if exception occurs:
– aman
Mar 25 at 6:19
Here is the code that I changed:
– aman
Mar 25 at 6:20
Here is the code that I changed:
– aman
Mar 25 at 6:20
except (NoSuchWindowException, ElementNotVisibleException, StaleElementReferenceException) as e: driver.switch_to.window(driver.window_handles[0]) refresh.click() break
– aman
Mar 25 at 6:21
except (NoSuchWindowException, ElementNotVisibleException, StaleElementReferenceException) as e: driver.switch_to.window(driver.window_handles[0]) refresh.click() break
– aman
Mar 25 at 6:21
add a comment |
1 Answer
1
active
oldest
votes
This break
below renders the following line unreachable:
break
driver.switch_to.window(driver.window_handles[0]) # <--- unreachable
Here is a small example of what you might be going for, note this code throws:
sequence = ['first', 'second', 'third']
def run_after_type_error_exception():
print("runs after type error")
def run_after_index_error_exception():
print("runs after index error")
Without break
s this code will catch both exceptions on the first iteration. Here you also have a for-else
block. Note, that after the third iteration this code will Do something else
.
for iteration in range(5):
for element in sequence:
try:
while sequence:
for character in element:
sequence.pop()
sequence[1].split() + 1
except (NameError, TypeError, ValueError) as e:
print(f"Caught first exception: e")
run_after_type_error_exception()
# break
except IndexError as e:
print(f"Caught exception e")
run_after_index_error_exception()
# break
else:
print("Do something else")
print(f"Current iteration: iteration")
Also, note time.sleep(is_in_seconds)
so if your code behaves similarly to the above code (where it's in the else
portion ~half of the time), then you'll be sleep
ing for ~4 minutes at least...
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332000%2fpython-code-not-running-during-an-exception-within-a-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This break
below renders the following line unreachable:
break
driver.switch_to.window(driver.window_handles[0]) # <--- unreachable
Here is a small example of what you might be going for, note this code throws:
sequence = ['first', 'second', 'third']
def run_after_type_error_exception():
print("runs after type error")
def run_after_index_error_exception():
print("runs after index error")
Without break
s this code will catch both exceptions on the first iteration. Here you also have a for-else
block. Note, that after the third iteration this code will Do something else
.
for iteration in range(5):
for element in sequence:
try:
while sequence:
for character in element:
sequence.pop()
sequence[1].split() + 1
except (NameError, TypeError, ValueError) as e:
print(f"Caught first exception: e")
run_after_type_error_exception()
# break
except IndexError as e:
print(f"Caught exception e")
run_after_index_error_exception()
# break
else:
print("Do something else")
print(f"Current iteration: iteration")
Also, note time.sleep(is_in_seconds)
so if your code behaves similarly to the above code (where it's in the else
portion ~half of the time), then you'll be sleep
ing for ~4 minutes at least...
add a comment |
This break
below renders the following line unreachable:
break
driver.switch_to.window(driver.window_handles[0]) # <--- unreachable
Here is a small example of what you might be going for, note this code throws:
sequence = ['first', 'second', 'third']
def run_after_type_error_exception():
print("runs after type error")
def run_after_index_error_exception():
print("runs after index error")
Without break
s this code will catch both exceptions on the first iteration. Here you also have a for-else
block. Note, that after the third iteration this code will Do something else
.
for iteration in range(5):
for element in sequence:
try:
while sequence:
for character in element:
sequence.pop()
sequence[1].split() + 1
except (NameError, TypeError, ValueError) as e:
print(f"Caught first exception: e")
run_after_type_error_exception()
# break
except IndexError as e:
print(f"Caught exception e")
run_after_index_error_exception()
# break
else:
print("Do something else")
print(f"Current iteration: iteration")
Also, note time.sleep(is_in_seconds)
so if your code behaves similarly to the above code (where it's in the else
portion ~half of the time), then you'll be sleep
ing for ~4 minutes at least...
add a comment |
This break
below renders the following line unreachable:
break
driver.switch_to.window(driver.window_handles[0]) # <--- unreachable
Here is a small example of what you might be going for, note this code throws:
sequence = ['first', 'second', 'third']
def run_after_type_error_exception():
print("runs after type error")
def run_after_index_error_exception():
print("runs after index error")
Without break
s this code will catch both exceptions on the first iteration. Here you also have a for-else
block. Note, that after the third iteration this code will Do something else
.
for iteration in range(5):
for element in sequence:
try:
while sequence:
for character in element:
sequence.pop()
sequence[1].split() + 1
except (NameError, TypeError, ValueError) as e:
print(f"Caught first exception: e")
run_after_type_error_exception()
# break
except IndexError as e:
print(f"Caught exception e")
run_after_index_error_exception()
# break
else:
print("Do something else")
print(f"Current iteration: iteration")
Also, note time.sleep(is_in_seconds)
so if your code behaves similarly to the above code (where it's in the else
portion ~half of the time), then you'll be sleep
ing for ~4 minutes at least...
This break
below renders the following line unreachable:
break
driver.switch_to.window(driver.window_handles[0]) # <--- unreachable
Here is a small example of what you might be going for, note this code throws:
sequence = ['first', 'second', 'third']
def run_after_type_error_exception():
print("runs after type error")
def run_after_index_error_exception():
print("runs after index error")
Without break
s this code will catch both exceptions on the first iteration. Here you also have a for-else
block. Note, that after the third iteration this code will Do something else
.
for iteration in range(5):
for element in sequence:
try:
while sequence:
for character in element:
sequence.pop()
sequence[1].split() + 1
except (NameError, TypeError, ValueError) as e:
print(f"Caught first exception: e")
run_after_type_error_exception()
# break
except IndexError as e:
print(f"Caught exception e")
run_after_index_error_exception()
# break
else:
print("Do something else")
print(f"Current iteration: iteration")
Also, note time.sleep(is_in_seconds)
so if your code behaves similarly to the above code (where it's in the else
portion ~half of the time), then you'll be sleep
ing for ~4 minutes at least...
answered Mar 25 at 7:20
ericeric
512138
512138
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332000%2fpython-code-not-running-during-an-exception-within-a-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your description of what is happening is very confusing. Also, it seems like trouble that you're calling the same line of code in your first except block that likely caused the exception in the first place. I'm not sure what's actually happening, but one thing I wonder is if where you have "continue", maybe you mean "break"? - the continue could very well explain why you're not "moving to the main loop".
– Steve
Mar 25 at 6:11
@Steve, I have tried with adding break in the exception, still the code under exception is not executing, basically I am trying to execute a code if exception occurs:
– aman
Mar 25 at 6:19
Here is the code that I changed:
– aman
Mar 25 at 6:20
except (NoSuchWindowException, ElementNotVisibleException, StaleElementReferenceException) as e: driver.switch_to.window(driver.window_handles[0]) refresh.click() break
– aman
Mar 25 at 6:21