0

I need to find a way to start the selenium webdriver only on demand and with the button 'Start'. when I do so and I launch the web_test function, I get the Error : 'name 'web_driver' is not defined'.

I can only make it work when the webdriver is launched at start(web_driver = webdriver.Edge('msedgedriver.exe') line 6 in the following exemple).

from tkinter import *
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


##web_driver = webdriver.Edge('msedgedriver.exe')

def start_webdriver():
    web_driver = webdriver.Edge('msedgedriver.exe')

def stop_webdriver():
    web_driver.quit()

def web_test():
    web_driver.get('https://stackoverflow.com/')
    web_driver.find_element_by_name('q').send_keys('python selenium')
    web_driver.find_element_by_name('q').send_keys(Keys.ENTER)
    

CanvasWd = 500
CanvasHg = 300

root = Tk()
root.title("app")
root.geometry(f"{CanvasWd}x{CanvasHg}")


canvas0 = Canvas(root,highlightthickness=0,width=CanvasWd,height=CanvasHg)

web_start_btn = Button(root,text='Start',command=start_webdriver)

web_stop_btn = Button(canvas0,text='Stop',command=stop_webdriver)

web_test_btn = Button(canvas0,text='testing',command=web_test)


canvas0.create_window(30, 10, anchor=NW, window=web_start_btn)
canvas0.create_window(10, 45, anchor=NW, window=web_stop_btn)
canvas0.create_window(90, 45, anchor=NW, window=web_test_btn)

canvas0.grid(row=1, column=1)

root.mainloop()

1 Answers1

1

I need to find a way to start the selenium webdriver only on demand and with the button 'Start'

To do this define a class, then you can simply call the associated method, such as start_webdriver to start the web driver, or main which is a method that contains all of the tkinter code.

any variable you define inside of the __init__ method can be accessed by other methods of the class, which would fix the issue you where having the web driver is not defined.

from tkinter import *
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


##web_driver = webdriver.Edge('msedgedriver.exe')

class driver():
    def __init__(self):
        self.CanvasWd = 500
        self.CanvasHg = 300
        self.root = Tk()
        self.root.title("app")
        self.root.geometry(f"{self.CanvasWd}x{self.CanvasHg}")
    
    def start_webdriver(self):
        self.web_driver = webdriver.Edge('msedgedriver.exe')

    def stop_webdriver(self):
        self.web_driver.quit()

    def web_test(self):
        self.web_driver.get('https://stackoverflow.com/')
        self.web_driver.find_element_by_name('q').send_keys('python selenium')
        self.web_driver.find_element_by_name('q').send_keys(Keys.ENTER)

    def main(self):
        canvas0 = Canvas(self.root,highlightthickness=0,width=self.CanvasWd,height=self.CanvasHg)

        self.web_start_btn = Button(self.root,text='Start',command=self.start_webdriver)

        self.web_stop_btn = Button(canvas0,text='Stop',command=self.stop_webdriver)

        self.web_test_btn = Button(canvas0,text='testing',command=self.web_test)


        canvas0.create_window(30, 10, anchor=NW, window=self.web_start_btn)
        canvas0.create_window(10, 45, anchor=NW, window=self.web_stop_btn)
        canvas0.create_window(90, 45, anchor=NW, window=self.web_test_btn)

        canvas0.grid(row=1, column=1)

        self.root.mainloop()

d = driver()
d.main()
  • This code will raise the same error that OP got. In the `web_test` method, `web_driver` isn't defined. Also if OP isn't using classes, it is safe to assume that their project isn't big or they haven't learned it yet. – TheLizzard Jun 19 '21 at 11:22
  • @TheLizzard webdriver is defined as a class variable `self.web_driver`, so no it shouldn't –  Jun 19 '21 at 11:25
  • You just [edited your answer](https://stackoverflow.com/posts/68046294/revisions) :D. – TheLizzard Jun 19 '21 at 11:26
  • @Jared, i copy pasted your class proposition and it didnt work, the app has no buttons and i get an error 'start_webdriver() takes 0 positional arguments but 1 was given' – ZOUHAIRI Fouad Jun 19 '21 at 11:27
  • @jared, the 'd.start_webdriver() ' need to be deleted so the webdriver only starts with the button start – ZOUHAIRI Fouad Jun 19 '21 at 11:43
  • @ZOUHAIRIFouad remove the `d.start_webdriver()` line –  Jun 19 '21 at 11:46
  • @ZOUHAIRIFouad if the answer works then please feel free to accept it, else tell me what you would like me to do too improve it. –  Jun 19 '21 at 11:47
  • @Jared You know OP's problem was solved 40 min ago by my comment on the question :D – TheLizzard Jun 19 '21 at 11:50
  • @TheLizzard global variables can cause a lot of issues, and are generally bad programming practice. Using classes is the proper way to do it, if you look at the source code for any module made for python, where it be from pip, or any major programming project. they tend to use classes and methods. see here https://stackoverflow.com/questions/19158339/why-are-global-variables-evil –  Jun 19 '21 at 11:55
  • @TheLizzard But thanks for your comment though :D –  Jun 19 '21 at 11:57
  • @Jared Given that OP hasn't used classes, I assumed that OP doesn't know how to use them or the project is small. Also there is no problem with global variables as long as you remember their names and contents. All modules that are bit use classes. Look at the standard python library: `tty`. It only uses functions and no classes because there is no point to using classes there. – TheLizzard Jun 19 '21 at 11:58