セレンを使用してwebfromを埋めるこの機能があります
def fill_form(self, action, dict):
import time
print "Starting %s ... \n" % action
# fill the form
for key in dict.keys():
try:
inputElement = self._driver.find_element_by_name(key)
inputElement.clear()
inputElement.send_keys(dict[key])
time.sleep(0.5)
except:
self.log()
pass
もちろん、この関数はクラスの一部です
この関数では、セレンはドロップダウンメニューを無視するため、各ドロップダウンを埋めてから、常に通常のステートメントを使用する必要がありますが、たとえばループの前に:
inputElement = driver.find_element_by_name('dropdownMenu')
inputElement.send_keys('Test')
object.fill_form('action here', dict)
関数なしでループを使用しようとしましたが、同じ結果が得られます。問題はループです!
ログファイルにも多くの例外があります
[2013-14-01 21:51:55] (<class 'selenium.common.exceptions.NoSuchElementException'>, NoSuchElementException(), <traceback object at 0x0212BFA8>)
[2013-14-01 21:52:04] (<class 'selenium.common.exceptions.NoSuchElementException'>, NoSuchElementException(), <traceback object at 0x021372B0>)
[2013-14-01 21:52:04] (<class 'selenium.common.exceptions.NoSuchElementException'>, NoSuchElementException(), <traceback object at 0x021431C0>)
[2013-14-01 21:52:51] (<class 'selenium.common.exceptions.WebDriverException'>, WebDriverException(), <traceback object at 0x0212FE90>)
フルクラス
# -*- coding: utf-8 -*-
"""
* *
* PyWebBot Automated Web Tasks based on Selenium using dictionaries *
* *
Features:
(1) - Login function
(2) - Visit Link
(3) - Make Screenshot
(4) - Fill and sumbit forms
Filename: ClassPyWebBot
Author: Isaak Wahb (Wahb Ben Ishak)
Contact: ben.ishak@tum.de
Date: 2012-08-15
Version: 1.0
Licence: GPL v2
Usage:
bot = PyWebBot([Selenium Webdriver] driver,
[dict(username,password)] user,
[String] _LogPath,
[String] _ScreenshotPath)
bot.run([String] action,
[dict] dict,
[String] baseurl,
[String] suburl)
"""
class PyWebBot():
"""
PyWebBot Automated Web Tasks Class
"""
def __init__(self,_driver, _user, _LogPath, _ScreenshotPath):
object.__init__(self)
self._driver = _driver
self._user = _user
self._LogPath = _LogPath
self._ScreenshotPath = _ScreenshotPath
pass
def log(self):
import time, sys, os
path = os.path.join(self._LogPath)
logfile = os.path.join(path,'error_log').replace(os.sep,'/')
time_x = time.strftime("%Y-%d-%m %H:%M:%S", time.gmtime(time.time()))
with open(logfile,'a') as log:
return log.write("[%s] %s\n" % (time_x, str(sys.exc_info()).strip()))
def screenshot(self, imgname):
import os, time
try:
time.sleep(1)
self._driver.get_screenshot_as_file(os.path.join(self._ScreenshotPath,imgname))
except:
self.log()
pass
def login(self):
import time
try:
inputElement = self._driver.find_element_by_name('username')
inputElement.send_keys(self._user['username'])
inputElement = self._driver.find_element_by_name('password')
inputElement.send_keys(self._user['password'])
self.screenshot('user_login_%s__%s.png' % (self._user['username'], time.time()))
inputElement.submit()
except:
self.log()
pass
def visit_link(self, baseurl, suburl):
import time
try:
self._driver.get("%s/%s" %(baseurl, suburl))
# Capture screen after visit
self.screenshot('visted_link_%s_%s__%s.png' % (suburl.replace('/','_'), self._user['username'], time.time()))
except:
self.log()
pass
def fill_form(self, action, dict):
import time
print "Starting %s ... \n" % action
# fill the form
for key in dict.keys():
try:
inputElement = self._driver.find_element_by_name(key)
inputElement.clear()
inputElement.send_keys(dict[key])
time.sleep(0.5)
except:
self.log()
pass
# capture screen before
self.screenshot('%s_%s__%s.png' % (action, self._user['username'], time.time()))
# submit the form
try:
inputElement.submit()
except:
pass
# Capture screen after submit
self.screenshot('%s_%s_submited__%s.png' % (action, self._user['username'], time.time()))
print "%s done.\n" % action
def run(self, action , dict, baseurl, suburl, login=True):
try:
# Visit link
self.visit_link(baseurl, suburl)
# need to login ?
if login: self.login()
# Fill form
self.fill_form(action, dict)
# Close driver
print "Finished\n"
self._driver.close()
pass
except:
self.log()
print "Error ... check error_log"