I m trying to automate login into a website whose login form has the below HTML code(excerpt):
<tr>
<td width="60%">
<input type="text" name="username" class="required black_text" maxlength="50" value="" />
</td>
<td>
<input type="password" name="password" id="password" class="required black_text" maxlength="50" value="" />
</td>
<td colspan="2" align="center">
<input type="image" src="gifs/login.jpg" name="Login2" value="Login" alt="Login" title="Login"/>
</td>
</tr>
I m using python's mechanize
module for web browsing.Following is the code:
br.select_form(predicate=self.__form_with_fields("username", "password"))
br['username'] = self.config['COMMON.USER']
br['password'] = self.config['COMMON.PASSWORD']
try:
request = br.click(name='Login2', type='image')
response = mechanize.urlopen(request)
print response.read()
except IOError, err:
logger = logging.getLogger(__name__)
logger.error(str(err))
logger.debug(response.info())
print str(err)
sys.exit(1)
def __form_with_fields(self, *fields):
""" Generator of form predicate functions. """
def __pred(form):
for field_name in fields:
try:
form.find_control(field_name)
except ControlNotFoundError, err:
logger = logging.getLogger(__name__)
logger.error(str(err))
return False
return True
return __pred
Not sure what am I doing wrong...
Thanks
There could be a possibility that the site uses java-script to do post back during login . I remember well that for ASP .Net sites you needed to get hold of HIDDEN FORM fields like VIEWSTATE and EVENTTARGET and post them across to the new Page . Why don't you send the link to the site in Question ? It becomes relatively easier to figure out after that
Try using Selenium
and PhantomJS
from selenium import PhantomJS
import platform
if platform.system() == 'Windows': # .exe for Windows
PhantomJS_path = './phantomjs.exe'
else:
PhantomJS_path = './phantomjs'
service_args = [ # Proxy (optional)
'--proxy=<>',
'--proxy-type=http',
'--ignore-ssl-errors=true',
'--web-security=false'
]
browser = PhantomJS(PhantomJS_path, service_args=service_args)
browser.set_window_size(1280, 720) # Window size for screenshot (optional)
login_url = "<url_here>"
# Credentials
Username = "<insert>"
Password = "<insert>"
# Login
browser.get(login_url)
browser.save_screenshot('login.png')
print browser.current_url
browser.find_element_by_id("<username field id>").send_keys(Username)
browser.find_element_by_id("<password field id>").send_keys(Password)
browser.find_element_by_id("<login button id>").click()
print (browser.current_url)
browser.get(scrape_url)
print browser.page_source
browser.quit()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.