1

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

2
  • Is that your whole code?/Did you instantiate this class? If you don't instantiate your class, no object will be created and no code will be run. You need to create the object and run the method you want.
    – h4z3
    CommentedNov 4, 2021 at 15:05
  • Thanks a lot, could you show exactly how it should look? I'm just a beginner
    – Рома
    CommentedNov 4, 2021 at 15:45

2 Answers 2

1

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() 
0
    0

    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/") 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.