Home > Back-end >  Selenium program execution does not start using Python Class
Selenium program execution does not start using Python Class

Time:11-05

Here is the code:

class InstogramBot():

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.driver = webdriver.Chrome()
    def close_browser(self):
        self.driver.get("https://www.instagram.com/")
        time.sleep(5)
        name_input = self.driver.find_element_by_name("username")
        name_input.send_keys(username)
        time.sleep(2)
        password_input = self.driver.find_element_by_name("password")
        password_input.send_keys(password)
        time.sleep(2)
        self.driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]/button/div').click()
        time.sleep(7)
        self.driver.close()
        self.driver.quit()

Selenium doesn't show up and doesn't even open the web driver

Console snapshot:

enter image description here

CodePudding user response:

If you do not have a driver object, this

self.driver.get("https://www.instagram.com/")

should result in a compile-time error.

Fix :

Please download latest chromedriver from here

Latest stable release: ChromeDriver 95.0.4638.54

Once downloaded, put that into some directory.

and use it like this :

driver_path = r'C:\\Users\\userID\\*****\\Desktop\\Automation\\chromedriver.exe'
self.driver = webdriver.Chrome(driver_path)
self.driver.get("https://www.instagram.com/")

CodePudding user response:

While using Python you need to consider the following:

  • Python being an object oriented programming language, everything in Python is represented as an object along with its properties and methods.
  • A Class in Python is the object constructor i.e. the mechanism for creating the objects.

Your program

The code block you have written includes the definition of the Class and the only method.

To initiate a successful execution, you need to create an instance, i.e an object of the class InstogramBot()

bot = InstogramBot("Рома", "Рома")

Finally, you need to invoke the method close_browser() through the object.

bot.close_browser()

Solution

Your effective code block will be:

from selenium import webdriver

class InstogramBot():

    def __init__(self, username, password):
    self.username = username
    self.password = password
    self.driver = webdriver.Chrome()

    def close_browser(self):
    self.driver.get("https://www.instagram.com/")
    time.sleep(5)
    name_input = self.driver.find_element_by_name("username")
    name_input.send_keys(username)
    time.sleep(2)
    password_input = self.driver.find_element_by_name("password")
    password_input.send_keys(password)
    time.sleep(2)
    self.driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]/button/div').click()


bot = InstogramBot("Рома", "Рома")
bot.close_browser()
  • Related