0

I have this code:

from selenium import webdriver
from bs4 import BeautifulSoup as BS
from types import NoneType 


class Alluris(object):

    def __init__(self, username, password):
        self.username = username
        self.password = password

    def log_in(self):
        driver = 
webdriver.PhantomJS('C:\Users\V\Desktop\PY\web_scrape\phantomjs.exe')
        driver.get('https://alluris.han.nl')
        driver.find_element_by_id('username').send_keys(self.username)
        driver.find_element_by_id('password').send_keys(self.password)
        driver.find_element_by_xpath('//*
[@id="formfields"]/input[3]').click()
        soup = BS(driver.page_source, 'lxml')
        if type(soup.find('div', {'id' : 'errormessage'})) == NoneType:
            print 'Logged in successfully'
        else:
            print soup.find('div', {'id' : 'errormessage'}).text 

I want the log_in method to auto run when I create an instance of a class, for example:

Alluris('username', 'password')

Output should be: Logged in successfully or Incorrect username or password.

I don't want to run the log_in method manually like

 Alluris('username', 'password').log_in()
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
V.Anh
  • 497
  • 3
  • 8
  • 15

1 Answers1

3

You can do that by calling the method in your __init__ method:

class Alluris(object):

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.log_in()
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139