1

I started learning python + selenium a few days ago, and am struggling to quit the browser outside of my bbdc_check function. My goal is whenever I interrupt bbdc_check or it encounters an error, I would like to quit the existing browser to start from scratch.

I keep encountering errors with quitting the browser. The error message for driver.quit() is "TypeError: quit() missing 1 required positional argument: 'self'".

I have a nagging suspicion that I'm supposed to use a class here, which I tried loosely off this solution, but still could not get it to work. Any ideas are appreciated, thank you.

FYI, date_a and date_b are not defined here because I deleted a bunch of code redundant to this issue. Assume that line of code works.

import selenium
from selenium import webdriver
import time
import sys

breakloop = 0

def bbdc_check():
    global breakloop
    driver = webdriver.Chrome(r'C:\<some dir>\chromedriver.exe')
    driver.get('<a website>')

    # A bunch of code here to compare 2 different dates
    if (date_a < date_b):
        breakloop = 1
    else:
        driver.quit()
        time.sleep(600)
        
# The main while-loop to run the programme
while breakloop == 0:
    try:
        bbdc_check()

    # If I manually interrupt, kill the programme
    except KeyboardInterrupt:
        driver = webdriver.Chrome
        driver.quit()
        sys.exit()

    # If programme encounters error, try again from scratch
    except:
        driver = webdriver.Chrome
        driver.quit()
        time.sleep(30)
2
  • Why are you creating driver object again and again ? Commented Jun 29, 2021 at 18:17
  • @ cruisepandey Right. So I've tried removing the line driver = webdriver.Chrome from the exceptions, but it doesn't work. How would I avoid creating multiple driver objects? Commented Jun 30, 2021 at 0:21

1 Answer 1

0

Seems you are creating new object of driver in each blocks as well as your function bbdc_check(). Create a single driver instance and use the same.

Sign up to request clarification or add additional context in comments.

4 Comments

noted. I've tried removing the line driver = webdriver.Chrome from the exceptions, but it doesn't work. How do I reference the same driver instance outside of the original function?
@blimsiang, please use it in the same way as you have used breakloop...a global driver..or else you can also have driver after the function gets over and pass it as a parameter in the function...
Of course! How silly... That makes complete sense, and has fixed the code. Thanks for your help!
Thats great your code is fixed...Happy crawling!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.