Python Forum
how do i loop a "def ..." when error occurs?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do i loop a "def ..." when error occurs?
#1
i'm automating some of my website monitoring work with python+selenium. the relevant part of the code looks like this:

import time
import schedule
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

def job():

        browser = webdriver.Chrome('W:\selenium\chromedriver')

        browser.get('https://xxxxxx.com')
        browser.maximize_window()

        # logging in
        browser.find_element_by_id("name").send_keys("username")
        browser.find_element_by_id("pwd").send_keys("password")
        browser.find_element_by_id("btn_login").click()

        # ...blah blah blah...

        browser.close()

schedule.every().day.at("09:00").do(job)

while True:
        schedule.run_pending()
        time.sleep(1)
because the website is poorly built, the log-in elements have a chance to never load correctly no matter the amount of time given, only a refresh would correct them. that means, most of the time, the script runs fine. but in some cases, the code terminates with error message "no such element". i've looked into "try" statements but only know how to use it in execution phase. so is there a way to incorporate "try" in the definition statement? or is there a better way to loop the script on error? cheers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  OSError occurs in Linux. anna17 2 192 Mar-23-2024, 10:00 PM
Last Post: snippsat
  Com Error with macro within for loop due to creating new workbook in the loop Lastwizzle 0 1,341 May-18-2019, 09:29 PM
Last Post: Lastwizzle
  New df from existing when condition occurs Devilish 0 1,317 Jan-10-2019, 12:03 AM
Last Post: Devilish
  How to make Python stops for debugging when the error occurs? Tim 5 5,774 Feb-16-2018, 09:50 AM
Last Post: Tim
  script closed before i can see the error that occurs? wrestlemy69 9 9,154 Dec-02-2016, 01:40 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020