I needed to use same instance of selenium web driver across different python files. This is my base class.
class Automate(object):
def __init__(self):
self.options = webdriver.ChromeOptions()
self.options.add_argument('--user-data-dir=\\profile path')
self.driver = webdriver.Chrome(executable_path="\\driver path", options=self.options)
def get(self, url):
self.driver.get(url)
Now, when I try to instantiate an object for this class in two different files like temp.py and test.py,
In temp.py
import automate
driver1 = Automate()
driver1.get("google.com")
In test.py
import automate
driver2 = Automate()
driver2.get("google.com")
The result is opening of two seperate chrome windows. I want these two files to use only one instance of the web driver. I tried searching for answers, someone said to use singleton classes (I don't know much about singleton class).
Can someone please give me an example on how to use the single driver instance across multiple python files.
What I want: I need 1st file to open the browser and 2nd file to send commands like get, find elements by xpath. Also, I want the 2nd file to be re runnable without opening a new browser window.