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;








-1















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.










share|improve this question
























  • 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


















-1















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.










share|improve this question
























  • 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














-1












-1








-1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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













1 Answer
1






active

oldest

votes


















1














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 breaks 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 sleeping for ~4 minutes at least...






share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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 breaks 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 sleeping for ~4 minutes at least...






    share|improve this answer



























      1














      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 breaks 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 sleeping for ~4 minutes at least...






      share|improve this answer

























        1












        1








        1







        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 breaks 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 sleeping for ~4 minutes at least...






        share|improve this answer













        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 breaks 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 sleeping for ~4 minutes at least...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 25 at 7:20









        ericeric

        512138




        512138





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript