Home > Enterprise >  How to change self.driver.get() to only self.get() in class?
How to change self.driver.get() to only self.get() in class?

Time:04-22

I've been looking for an answer but cant find one

Let's say I have this,

self.driver =  webdriver.Chrome(options=self.options)

and then I use,

self.driver.get('http://www.google.com/')

Is there a way for me to use just 'self.' on the next method where everything from self.driver pops up? Like so,

self.get('http://www.google.com/')

CodePudding user response:

Create get-method for your class that calls driver.get. Then you can refer to it inside the class as self.get.

class CustomDriver:
    def __init__(self, driver):
        self.driver = driver

    def get(self, url):
        self.driver.get(url)

    def another_method(self):
        self.get("<some_url>")
        # do some other stuff here...

# initiate driver first
driver = ...

drv = CustomDriver(driver)
drv.get("<some_url>")

CodePudding user response:

self in Python class

self represents the instance of the class. By using the self keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments.

In short, self is always pointing to current object.

But get() is method from the WebDriver implementation. So to access get() you have to invoke it through the instance of the WebDriver i.e. driver which you have already initialized through:

self.driver =  webdriver.Chrome(options=self.options)
  • Related