'TypeError: Missing 1 required positional argument: 'self' Anybody?

I keep on getting issues with a part of my code. Anybody has some idea how to tackle it? Python - Selenium

I added the full code. The purpose is to auto open Instagram Stories.

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
import random
import string

#Chromedriver path. Make sure to have the same Chromedriver version as your Google Chrome browser
browser = webdriver.Chrome(executable_path= r"")  # <----- ENTER PATH HERE 

browser.get(('https://www.instagram.com/accounts/login/?source=auth_switcher'))
sleep(2) 
        
def start():
    acceptCookies = browser.find_element_by_xpath('/html/body/div[4]/div/div/button[1]');
    acceptCookies.click();
    sleep(4);
    #browser.implicitly_wait(3)  #this is another wait function.If you would like to run the script faster, change all sleep() to this
    username = browser.find_element_by_name('username')
    username.send_keys('') # <- INSERT YOUR INSTAGRAM USERNAME HERE -------------------------------------------------------------------------------------------------------------------------
    password = browser.find_element_by_name('password')
    password.send_keys('') # <- INSERT YOUR INSTAGRAM PASSWORD HERE -----------------------------------------------------------------------------------------------------------------------
    nextButton = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div/div[1]/div/form/div/div[3]/button')
    nextButton.click()
    #browser.quit()
    sleep(5)
    notification = browser.find_element_by_xpath("//button[contains(text(), 'Niet nu')]")
    notification.click()
    sleep(5)
    notification = browser.find_element_by_xpath("//button[contains(text(), 'Niet nu')]")
    notification.click()
    sleep(5)

def openStories(self):
    bot=self.bot
    bot.find_element_by_xpath('OE30K').click()  

    
#Start the programm
start()
openStories()


Solution 1:[1]

Hope you understand the difference between defining a method within a class and a function outside a class.

class MyClass:
    foo = "HELLO!"

    def openStories1(self):
        # Here self is an instance of MyClass
        print(self.foo)

def openStories2(self):
    # Here self is just an object.
    print(self.foo)

# Can be called without explicitly passing an argument, since self refers to the instance1
instance1 = MyClass()
instance1.openStories1()

# When defining outside a class you will need to pass the instance explicitly.
openStories2(instance1)

# Raises error since this function cannot be accessed through the instance because it's not defined in the class.
instance1.openStories2()

Outputs:

HELLO!
HELLO!
Traceback (most recent call last):
  File "scratch_6.py", line 18, in <module>
    instance1.openStories2()
AttributeError: 'MyClass' object has no attribute 'openStories2'

Solution 2:[2]

In Python self is used this way:

class MyClass:
    def __init__(self, name: str, age: int) -> None: # constructor
        self.age = age # self is the object that the constructor is initializing
        self.myMethod(name) # You can call methods passing the instance itself as argument, the argument 'self' is the instance
    def myMethod(self, name: str) -> None:
        self.name = name

The self variable represents the instance itself.

>>> my_object = MyClass()
>>> my_object.myMethod('myName')
>>> print(my_object.name)
'myName'

The self argument is the instance on which the method is called.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pinxau1000
Solution 2