1

I tried to write a open browser function inside a class like below to open facebook.

class Webdriver:
    def __init__(self):
        self.path = "C:/Users/chromedriver.exe"
        self.driver = webdriver.Chrome(path).get("https://www.facebook.com/")

After I called this function:

Webdriver().driver

The browser will be automatically closed after it is opened. I wrote a tkinter UI and I would like to call this function by clicking a button in that UI as illustrated below to initiate and open the browser continuously. However, the browser will be opened and closed immediately after I clicked the button. How can I keep the browser open so that I can call other function e.g logging in to facebook and do the searching.

root = tk.Tk()
driverinit = Button(root, text="Open the driver", command=lambda: Webdriver().driver).grid(row=5, column=1, columnspan=2, pady=10, padx=30)
root.mainloop()
Alex
  • 21
  • 1

1 Answers1

0

Are you familiar with Python breakpoints? You could drop one into your code so that your program pauses execution after reaching it.

There's a lot of info about that in this Stack Overflow post: Simpler way to put PDB breakpoints in Python code?

Eg:

import pdb; pdb.set_trace()

Once you are in a breakpoint, type c in the console and hit Enter to continue from where you left off. Or you can go line-by-line from there by using n and hitting Enter. Or to step into a method, use s and hit Enter.

If using breakpoints for debugging selenium tests that use pytest, there's also this post for reference: https://seleniumbase.com/the-ultimate-pytest-debugging-guide-2021/

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48