0

I have cable internet which requires me to login though a web page. It's annoying how it resets every day at 8 and 12am. I wanted to write a python script which will automate the login process. I've read many StackOverflow solutions so far, nothing has worked. I have tried Requests, Twill, RoboBrowser etc.

Upon inspecting the page source I came across a doLogin() ajax script, which is triggered by login button. Here is the full page source.

following is one of my implementations which fails

import requests

# Fill in your details here to be posted to the login form.
payload = {
    'action': 'http://10.10.0.1/login',
    'actualusername': 'username',
    'actualpassword': 'password'
}

# Use 'with' to ensure the session context is closed after use.
with requests.Session() as s:
    p = s.post("http://103.251.83.134/captiveportal/Default.aspx", data=payload)
    # print the html returned or something more intelligent to see if it's a successful login page.
    print p.text

    # An authorised request.
    #r = s.get('http://www.google.com')
    #print r.text

EDIT: Solution

I used Selenium WebDriver to fix this. Check answer.

Community
  • 1
  • 1
Dipanjan Patra
  • 59
  • 1
  • 3
  • 10

2 Answers2

1

Use Selenium :) Download ChromeDriver to the path, make a two-time variable and check the time every minute. If it's 'login time', your browser will pass through the authorization.

from selenium import webdriver

def Authorization_for_broadband():
    driver = webdriver.Chrome("C:\YOURPATHTO\CHROMEDRIVER.EXE")

    driver.get('http://10.10.0.1/login')
    driver.find_element_by_xpath('//*[@id="username"]').send_keys('USERNAME')
    driver.find_element_by_xpath('//*[@id="password"]').send_keys('PASSWORD')
    driver.find_element_by_xpath('//*[@id="btnLogin"]').click()
    driver.close

while(1):
    if time=='your-login-period1' or time == 'your-login-period2':
        Authorization_for_broadband()
Dead Community
  • 157
  • 1
  • 13
Attila Kis
  • 521
  • 2
  • 13
  • I exactly implemented this today, before even reading this answer! Thank you for posting this. I used webdriver.Firefox() though. Which works fine. I wanted the process to be silent, for that I'll try PhantomJS, as I read somewhere. I'll make this accepted solution if anyone needs! – Dipanjan Patra Apr 03 '17 at 12:41
0

Your URL may be wrong. Looking at the source code it looks like the HTML form is posting data to the http://10.10.0.1/login page and then the doLogin() function is submitting data to Register.aspx?CheckCustomerStatus=1.

Also your payload includes the variable action and you're using a Session object, which I don't think is necessary.

I can't test it since it's a local login page I can't access, but I would try modifying your code to submit the login info to both pages using a simpler POST request

heyon
  • 26
  • 3