def main(): br = Browser('chrome') br.visit(URL) sleep(3) if br.is_text_present('Connection', wait_time=7): br.fill('login', '...') br.fill('password', '...') br.find_by_css('#logonForm_connect_button').first.click()
def main(): br = Browser( 'chrome' ) #The Browser function takes the "Browser name" as an argument, it can be changed as per will. br.visit(URL) sleep(3) if br.is_text_present('Connection', wait_time=7): br.fill('login', NAME) br.fill('password', PASSWORD) br.find_by_css('#logonForm_connect_button').first.click()
def main(): executable_path = { 'executable_path': '/usr/lib/chromium-browser/chromedriver' } br = Browser('chrome', **executable_path) br.visit(URL) sleep(5) if br.is_text_present('Password', wait_time=7): br.fill('userpass', PASSWORD) br.find_by_css('#LoginButton').first.click() sleep(5) if br.is_text_present('Connect', wait_time=7): br.find_by_id('#netmode_mode').first.click() sleep(5)
def test_context_leak(): output_fn = JQUERY_MIN_PATH + '.ctx.src' ctx_fn = JQUERY_MIN_PATH + '.ctx.leak' obj_fn = JQUERY_MIN_PATH + '.ctx.obj' input_fn = os.path.join(PROJECT_DIR, JQUERY_PATH) if os.path.isfile(ctx_fn): os.remove(ctx_fn) p = subprocess.Popen(['java', '-jar', COMPRESSOR_PATH, '--leaktype', 'context', '--leakkey', KEY_PATH, '-o', ctx_fn, input_fn], shell=True) p.wait() from splinter.browser import Browser browser = Browser() browser.visit('file://' + os.path.realpath('test-ctx.html')) try: leak_json = browser.find_by_css('#leakObj')[0].value finally: browser.quit() if os.path.isfile(output_fn): os.remove(output_fn) open(obj_fn, 'wb').write(leak_json) p = subprocess.Popen(['java', '-jar', EXTRACTOR_PATH, '--leaktype', 'context', '--leakkey', KEY_PATH, '-o', output_fn, obj_fn], shell=True) p.wait() return open(JQUERY_PATH, 'rb').read() == open(output_fn, 'rb').read()
def test_context_leak(): output_fn = JQUERY_MIN_PATH + '.ctx.src' ctx_fn = JQUERY_MIN_PATH + '.ctx.leak' obj_fn = JQUERY_MIN_PATH + '.ctx.obj' input_fn = os.path.join(PROJECT_DIR, JQUERY_PATH) if os.path.isfile(ctx_fn): os.remove(ctx_fn) p = subprocess.Popen([ 'java', '-jar', COMPRESSOR_PATH, '--leaktype', 'context', '--leakkey', KEY_PATH, '-o', ctx_fn, input_fn ], shell=True) p.wait() from splinter.browser import Browser browser = Browser() browser.visit('file://' + os.path.realpath('test-ctx.html')) try: leak_json = browser.find_by_css('#leakObj')[0].value finally: browser.quit() if os.path.isfile(output_fn): os.remove(output_fn) open(obj_fn, 'wb').write(leak_json) p = subprocess.Popen([ 'java', '-jar', EXTRACTOR_PATH, '--leaktype', 'context', '--leakkey', KEY_PATH, '-o', output_fn, obj_fn ], shell=True) p.wait() return open(JQUERY_PATH, 'rb').read() == open(output_fn, 'rb').read()
def analyze(self): project_modules = self.get_project_modules() project_settings = self.project.source + self.project_root_path + "/settings.py" TEST_RUNNER = 'TEST_RUNNER = "django_nose.NoseTestSuiteRunner"' TESTS_APPS = "TESTS_APPS = ('django_nose',)" NOSE_ARGS = ['--quiet', "-sd", '--nologcapture', '--with-coverage', '--cover-erase', '--cover-html', '--cover-html-dir=' + self.project.source + self.project_root_path, '--with-spec', '--spec-color'] MODULES = ["--cover-package=" + module for module in project_modules] NOSE_ARGS.extend(MODULES) NOSE_ARGS = "NOSE_ARGS = " + str(NOSE_ARGS) settings_file = open(project_settings, "a") settings_file.write(TEST_RUNNER + "\n") settings_file.write(TESTS_APPS + "\n") settings_file.write(NOSE_ARGS + "\n") settings_file.close() os.system("python " + self.project.source + self.project_root_path + "/manage.py test") browser = Browser("zope.testbrowser") browser.visit("file://" + settings.PROJECT_ROOT + "/" + self.project.source + self.project_root_path + "/index.html") percent = browser.find_by_css("#index tfoot tr .right").text self._remove_extracted_code() return int(percent.replace("%", ""))
def main(): from splinter.browser import Browser browser = Browser() browser.visit('http://google.com') browser.fill('q', 'splinter - python acceptance testing for web applications') browser.find_by_css('.lsb').first.click() if browser.is_text_present('splinter.cobrateam.info'): print "Yes, the official website was found!" else: print "No, it wasn't found... We need to improve our SEO techniques" #browser.execute_script("var win = window.open(); win.document.write('<html><head><title>Generated HTML of ' + location.href + '</title></head><pre>' + document.documentElement.innerHTML.replace(/&/g, '&').replace(/</g, '<') + '</pre></html>'); win.document.close(); void 0;") browser.quit()
def _extract_infos(self): browser = Browser("zope.testbrowser") browser.visit("file://" + settings.PROJECT_ROOT + "/output.html" ) infos = browser.find_by_css("p")[3].text browser.quit() os.system("rm " + settings.PROJECT_ROOT + "/output.html") return infos
def run_specs(path): print 'Using %s as runner.' % path browser = Browser() browser.visit(path) runner_div = browser.find_by_css('.runner').first passed = 'passed' in runner_div['class'] if passed: color = 'green' exit_status = 0 else: color = 'red' exit_status = 1 output = browser.find_by_css('.runner span .description').first.text browser.quit() print colored(output, color) return exit_status
def getInfo(self): self.company = 'Discover' self.bureaus = 'TransUnion' try: browser = Browser(driver_name='firefox') browser.visit(self.url) browser.driver.set_window_size(self.window_size_x, self.window_size_y) browser.find_by_id(self.username_id).fill(self.username) browser.find_by_id(self.password_id).fill(self.password) browser.find_by_id(self.loginbutton_id).click() time.sleep(5) self.score = browser.find_by_css(self.score_class).text.encode('ascii','ignore') self.score = re.sub("[^0-9]", "", self.score) self.scoreDate = browser.find_by_css(self.date_class).text.encode('ascii','ignore') browser.quit() print(self.company + " succeeds!") except: print(self.company + " failed!") pdb.set_trace() if browser: browser.quit()
class metaCatcher: def __init__(self): self.browser = Browser() self.browser.driver.set_page_load_timeout(5) def set_url(self, url): self.url = url def download(self, url, name): if os.path.exists(name): return result = requests.get(url) if '</html>' in result.content: return output = open(name, 'w') output.write(result.content) output.close() def catch(self): self.browser.visit(self.url) items = self.browser.find_by_css('.dataset-heading') tmpitems = [] setName = [] for item in items: href = item.find_by_tag('a') setName.append(href[0].text) tmpitems.append(href[0]['href']) print setName i = -1 for href in tmpitems: i += 1 try: self.browser.visit(href) except TimeoutException: pass groups = self.browser.find_by_tag('a') for g in groups: #g.text downloadurl = str(g['href']) if g.text == 'Download Metadata': #print downloadurl #print downloadurl name = setName[i] self.download(downloadurl, './meta/Safety1/' + name + '.json')
class E2ETestCase(LiveServerTestCase): def setUp(self, *args, **kwargs): super(E2ETestCase, self).setUp(*args, **kwargs) self.browser = Browser(settings.BROWSER) self.browser.cookies.delete() def tearDown(self, *args, **kwargs): self.browser.quit() super(E2ETestCase, self).tearDown(*args, **kwargs) def visit(self, url): try: url = reverse(url) except: pass self.browser.visit("%s%s" % (self.live_server_url, url)) def ele(self, css_selector): return self.browser.find_by_css(css_selector).first def sleep(self, seconds): time.sleep(seconds)
class TestCase(ptc.PloneTestCase): def __init__(self): self.browser = Browser(driver_name='firefox') self.host, self.port = startZServer() def afterSetUp(self): self.browser.visit('http://%s:%s/plone' % (self.host, self.port)) def beforeTearDown(self): self.browser.quit() def portal_visit(self, url): self.browser.visit('http://%s:%s/plone/%s' % (self.host, self.port, url)) def portal_home(self): self.browser.visit('http://%s:%s/plone/' % (self.host, self.port)) def portal_login(self, user, password): self.portal_visit('login_form') self.browser.fill('__ac_name', user) self.browser.fill('__ac_password', password) self.browser.find_by_name('submit').first.click() def portal_login_as_owner(self): self.portal_login(user=portal_owner, password=default_password) def portal_logout(self): self.portal_visit('logout') def portal_search(self, search_word): self.browser.fill('SearchableText','%s' % (search_word)) self.browser.find_by_css('.searchButton').first.click() def portal_navigate_submenu(self, option): self.browser.find_by_xpath("//li[contains(@id, 'contentview')]/a[text()='%s']" % (option)).first.click() def portal_click_a_personaltool(self, personaltool): self.browser.click_link_by_href('http://%s:%s/plone/dashboard' % (self.host, self.port)) self.browser.click_link_by_text('%s' % (personaltool)) def portal_add_user(self, fullname, username, email, password): self.portal_click_a_personaltool('Site Setup') self.browser.click_link_by_text('Users and Groups') self.browser.find_by_name('form.button.AddUser').first.click() self.browser.fill('form.fullname','%s' % (fullname)) self.browser.fill('form.username','%s' % (username)) self.browser.fill('form.email','%s' % (email)) self.browser.fill('form.password','%s' % (password)) self.browser.fill('form.password_ctl','%s' % (password)) self.browser.find_by_id('form.actions.register').first.click() def portal_add_user_as_manager(self, fullname, username, email, password): self.portal_click_a_personaltool('Site Setup') self.browser.click_link_by_text('Users and Groups') self.browser.find_by_name('form.button.AddUser').first.click() self.browser.fill('form.fullname','%s' % (fullname)) self.browser.fill('form.username','%s' % (username)) self.browser.fill('form.email','%s' % (email)) self.browser.fill('form.password','%s' % (password)) self.browser.fill('form.password_ctl','%s' % (password)) self.browser.find_by_id('form.groups.0').first.click() self.browser.find_by_id('form.actions.register').first.click() def portal_change_user_role(self, username, new_role): self.portal_click_a_personaltool('Site Setup') self.browser.click_link_by_text('Users and Groups') self.browser.find_by_xpath("//tr[*/input[@value='%s']]//input[@value='%s']" % (username, new_role)).first.click() self.browser.find_by_name('form.button.Modify').first.click() def portal_click_enable_content_types(self): self.browser.find_by_css('a[title="Add new items inside this item"]').first.click() def portal_add_content_type(self, type): self.portal_click_enable_content_types() self.browser.click_link_by_text('%s' % (type)) def portal_click_content_item_action(self): self.browser.find_by_css('a[title="Actions for the current content item"]').first.click() def portal_add_item_action(self, type): self.portal_click_content_item_action() self.browser.click_link_by_text('%s' % (type)) def portal_list_states(self): self.browser.find_by_css('a[title="Change the state of this item"]').first.click() def portal_modify_state_to(self, state): self.portal_list_states() self.browser.click_link_by_text('%s' % (state))
from splinter.browser import Browser browser = Browser() browser.visit('http://google.com') browser.fill('q', 'splinter - python acceptance testing for web applications') browser.find_by_css('.lsb').first.click() if browser.is_text_present('http://splinter.cobrateam.info'): print "Yes, the official website was found!" else: print "No, it wasn't found... We need to improve our SEO techniques" browser.quit()
""" interact with forms, test if element exists in a page This example: - Try to login on Google Accounts - do this over and over again - until the captcha appear Just for try if after many attempts to login, Google show the captcha for the safe of the system. """ from splinter.browser import Browser browser = Browser('chrome') browser.visit('https://www.google.com/accounts/ServiceLogin') # just a counter i = 0 # while not appear the captcha input, try login with wrong account while True: browser.fill('Email', 'my_wrong_username') browser.fill('Passwd', 'my_wrong_password') browser.find_by_css('#signIn').first.click() i += 1 if browser.is_element_present_by_id('logincaptcha'): break print 'After', i, 'attempts the captcha appeared!'
This example will works perfectly if you completed the second part of the django tutorial: https://docs.djangoproject.com/en/dev/intro/tutorial02/ For more information, just check: https://docs.djangoproject.com/en/dev/intro/tutorial01/ NOTE: This was tested when the version of the tutorial was for Django 1.3 """ from splinter.browser import Browser browser = Browser() # You may change this url to the current url from your Poll App browser.visit('http://127.0.0.1:8000/admin/') # You may change the username and password too browser.fill('username', 'douglas') browser.fill('password', '123456') # do login browser.find_by_css('.submit-row input').first.click() # add a new poll browser.find_link_by_href('polls/poll/add/').first.click() browser.fill('question', 'Does this test is completed successfully?') # if you follow the instructions from the tutorial, this is will show # the datetime options from the poll browser.find_by_css('.collapse-toggle').first.click() datetime_fields = browser.find_by_css('.datetimeshortcuts') datetime_fields[0].find_by_tag('a').first.click() datetime_fields[1].find_by_tag('a').first.click() # fill the choices browser.fill('choice_set-0-choice', 'Yup!') browser.fill('choice_set-0-votes', '0') browser.fill('choice_set-1-choice', 'Nope!') browser.fill('choice_set-1-votes', '0')
""" This example shows how to test web interface with splinter API with mouse interactions. In this page (http://douglasmiranda.com/labs/projetos/) when the mouse is over an element, it shows a description of itself. So let's automate the test to verify if this is ok. PS: This is a mouse interaction, so when you execute this script, you should stop moving the mouse :) """ from splinter.browser import Browser browser = Browser('chrome') browser.visit('http://douglasmiranda.com/labs/projetos/') li = browser.find_by_css('#ultimos-projetos li').first li.mouse_over() print 'When the mouse is over the description is visible? ', li.find_by_css( '.descricao').visible li.mouse_out() print '...and when the mouse is out the description is visible? ', li.find_by_css( '.descricao').visible browser.quit()
FAIL = '\033[91m' ENDC = '\033[0m' stickerList = open('msnSticker.txt') #open the the text file allStickers = stickerList.readlines() #read all resultFile = open('incomeResult.txt', "w") #report result for eachSticker in allStickers: print 'Income Statement of %s' % eachSticker, print '--------------------------------------------------------------------------------------------------' b = Browser('chrome') #create a browser instance loginURL = 'http://investing.money.msn.com/investments/stock-income-statement/?symbol=%s' % eachSticker b.visit(loginURL) #login URL year = b.find_by_css("tr.mnrow1") #find year allBold = b.find_by_css("tr.mnboldtopline") #find all bold lines netIncome = b.find_by_css("tr.mnborderbottop3line") #net income yearRaw = year.value #print year yearRaw = re.sub(' ', "\t\t", yearRaw) print "Year:\t\t\t%s" % yearRaw totalRevRaw = allBold[0].value #print total revenue totalRevArray = totalRevRaw.split("\n") totalRevArray[1] = re.sub(' ', "\t", totalRevArray[1]) print "Total Revenue:\t\t%s" % totalRevArray[1] grossProfitRaw = allBold[1].value #print gross profit grossProfitArray = grossProfitRaw.split("\n") grossProfitArray[1] = re.sub(' ', "\t", grossProfitArray[1])
# estou inserindo este delay aqui porque ao preencher o campo de busca # há uma animação no botão "Pesquisar", este é o tempo para que # a animação esteja completa e o botão esteja clicável # que é um ponto importante, pois o objeto precisa estar visível para # que ele possa ser clicado time.sleep(0.2) # Encontrando o botão de submit do formulário # que é um <input type="submit"... dentro de um # formulário que tem o id="search-form" # por um seletor CSS, se você é familiarizado # com CSS vai achar bem simples o uso # mais formas de encontar elementos na página em: # http://splinter.cobrateam.info/docs/finding.html botao_pesquisar = browser.find_by_css("#search-form input[type=submit]") # Veja que ao encontrar o botão eu posso interagir com ele # neste caso "estou clicando" nele, que vai submeter meu # formulário #search-form, enviando a palavra "python" para ser # pesquisada em meu banco de dados botao_pesquisar.click() # com o método is_element_present_by_id vou checar se apareceu # a lista de posts que tem o id="posts-list" # mais formas checar textos e elementos presentes no corpo da página em: # http://splinter.cobrateam.info/docs/matchers.html if browser.is_element_present_by_id('posts-list'): print "Oh yes! Já escrevi artigos sobre '%s'! =D" % palavra_chave else: print "Não escrevi nenhum artigo sobre '%s'? OMG!" % palavra_chave
EMAIL = '*****@*****.**' #Define o email que deve digitar SENHA = 'Teste1234' #Define a senha que deve digitar OBSERVACAO = 'Teste automatizado Peeksello.' SLEEP = 4 #Tempo determinado para aguardar antes de executar a próxima linha COLECAO = 2 #Seleciona coleção específica CATEGORIA = 'Camiseta' #Seleciona categoria específica QTD = 5 #Incrementar quantidade - definir valor #PESQUISACLIENTE = 'Lucy' #Digita nome de cliente específico (no mínimo 3 caracteres) #EMAIL = '*****@*****.**' #SENHA = 'Peeksello1234' #Realizar login com conta login e senha B = Browser('firefox', capabilities={'acceptSslCerts': True}) B.visit(URL) #Visita a url time.sleep(SLEEP) B.find_by_css('a.btn-sign-in').last.click() #Clicar em Outra Conta time.sleep(SLEEP) B.find_by_css('.form-group .form-control').click() #Clicar no campo Login B.find_by_css('.form-group .form-control').type( EMAIL) #Digita o email definido #na variável time.sleep(SLEEP) B.find_by_css('.form-group .form-control').last.click() #Clicar no campo Senha time.sleep(SLEEP) B.find_by_css('.form-group .form-control').last.type( SENHA) #Digita a senha definida #na variável time.sleep(SLEEP) B.find_by_css('input.btn.btn-primary').click() #Clica em login """#Realizar login com conta Google B = Browser('firefox', capabilities={'acceptSslCerts': True})
class TestEngine(object): __sleep_time = 2 __mouse_over = True __mouse_over_sleep = 1 def __init__(self, browser_name, execute_path=None): if execute_path is None: self.__browser = Browser(browser_name, fullscreen=True) self.__quit = False else: self.__browser = Browser(browser_name, executable_path=execute_path, fullscreen=True) self.__quit = False @staticmethod def set_config(config): TestEngine.__sleep_time = 2 if config.get("sleep_time") is None else config.get("sleep_time") TestEngine.__mouse_over = True if config.get("mouse_over") is None else config.get("mouse_over") TestEngine.__mouse_over_sleep = 1 if config.get("mouse_over_sleep") is None else config.get("mouse_over_sleep") def test_list_acts(self, domain, action_list, back_fun=None, result_back=None): thread_deal = threading.Thread(target=self.__test_list_thread, args=(domain, action_list, back_fun, result_back), name="TestEngine deal tester") thread_deal.start() def test_deal(self, domain, action_obj, back_fun=None, result_back=None): thread_deal = threading.Thread(target=self.__test_do_thread, args=(domain, action_obj, back_fun, result_back), name="TestEngine deal tester") # hasattr(result_back, "__call__") thread_deal.start() def quit(self): self.__quit = True self.__browser.quit() def is_quited(self): return self.__quit def __test_list_thread(self, domain, action_list, back_fun=None, result_back=None): try: for action in action_list: self.__test_do(domain, action, result_back) except Exception as e: raise Exception("[Error code] deal test list failed, error code=", e) finally: if action_list[0].waitClose != 0: sleep(action_list[0].waitClose) if back_fun is None: self.quit() else: back_fun() def __test_do_thread(self, domain, action_obj, back_fun=None, result_back=None): try: self.__test_do(domain, action_obj, result_back) except Exception as e: raise Exception("[Error code] deal test failed, error code=", e) finally: if action_obj.waitClose != 0: sleep(action_obj.waitClose) if back_fun is None: self.quit() else: back_fun() def __test_do(self, domain, action_obj, result_back=None): test_url = domain+action_obj.urlPath self.__browser.visit(test_url) # form表单默认为第一个action循环测试,之后的action按照顺序执行 action_list = TesterActionData().dict_to_list(action_obj.actionList) if action_obj.forms is not None: form_action = action_list[0] if action_list else None forms = TesterForms().dict_to_list(action_obj.forms) for form in forms: params = TesterFormData().dict_to_list(form.params) for param in params: self.__set_value(int(param.formType), param.formElName, param.formElValue.decode("utf-8"), int(param.index)) sleep(TestEngine.__sleep_time) if form_action is not None: self.__deal_action(form_action, result_back) sleep(action_obj.sleepTime) for action_deal in action_list[1:]: self.__deal_action(action_deal, result_back) sleep(action_obj.sleepTime) else: for action_deal in action_list: self.__deal_action(action_deal, result_back) sleep(action_obj.sleepTime) def __set_value(self, form_type, el_name, el_value, index): elements = self.__event_element(form_type, el_name) element = elements[index] if element['type'] in ['text', 'password', 'tel'] or element.tag_name == 'textarea': element.value = el_value elif element['type'] == 'checkbox': if el_value: element.check() else: element.uncheck() elif element['type'] == 'radio': element.click() elif element._element.tag_name == 'select': element.find_by_value(el_value).first._element.click() else: element.value = el_value def __event_element(self, el_type, el_value): ele_type = EL_TYPE.value(el_type) if ele_type == "id": return self.__browser.find_by_id(el_value) elif ele_type == "name": return self.__browser.find_by_name(el_value) elif ele_type == "tag": return self.__browser.find_by_tag(el_value) elif ele_type == "value": return self.__browser.find_by_value(el_value) elif ele_type == "selector": return self.__browser.find_by_xpath(el_value) elif ele_type == "css": return self.__browser.find_by_css(el_value) else: raise ValueError("Test Engine can't deal the element type:%s, el_type:%s", ele_type, el_type) def __deal_action(self, action_data, result_back=None): action_type = ACTION_TYPE.value(action_data.action) # 当页面跳转是抓取最后一个打开的窗口页面 self.__browser.windows.current = self.__browser.windows[-1] if action_type == "click": self.__mouse_of_click(self.__event_element(action_data.elType, action_data.elValue)[int(action_data.index)]) elif action_type == "double click": self.__mouse_of_double_click(self.__event_element(action_data.elType, action_data.elValue)[int(action_data.index)]) elif action_type == "right click": self.__mouse_of_right_click(self.__event_element(action_data.elType, action_data.elValue)[int(action_data.index)]) elif action_type == "mouse over": self.__event_element(action_data.elType, action_data.elValue)[int(action_data.index)].mouse_over() elif action_type == "mouse out": self.__event_element(action_data.elType, action_data.elValue)[int(action_data.index)].mouse_out() elif action_type == "select": self.__event_element(action_data.elType, action_data.elValue)[int(action_data.index)].select() else: raise Exception("don't find action for action:%s", action_data.action) try: if action_data.testerResult is not None and result_back is not None: sleep(3) result_back(TesterResult(action_data.testerResult, self.__browser.is_text_present(action_data.testerResult))) except Exception: result_back(TesterResult(action_data.testerResult, False)) def __mouse_of_click(self, event_deal_obj): if TestEngine.__mouse_over: event_deal_obj.mouse_over() sleep(TestEngine.__mouse_over_sleep) event_deal_obj.click() else: event_deal_obj.click() def __mouse_of_right_click(self, event_deal_obj): if TestEngine.__mouse_over: event_deal_obj.mouse_over() sleep(TestEngine.__mouse_over_sleep) event_deal_obj.right_click() else: event_deal_obj.click() def __mouse_of_double_click(self, event_deal_obj): if TestEngine.__mouse_over: event_deal_obj.mouse_over() sleep(TestEngine.__mouse_over_sleep) event_deal_obj.double_click() else: event_deal_obj.click()
class ticket(): # 浏览器 driver_name = None executable_path = None # 用户名,密码 username = None passwd = None # 票信息 starts = None ends = None dtime = None order = None userNames = None # 席位 xb = None # 几等座 pz = None # 票类型:成人 学生 # 12306 urls ticket_url = None login_url = None initmy_url = None buy_url = None # driver driver = None time_interval = None def __init__(self, driver_name, executable_path, username, passwd, starts, ends, dtime, order, userNames, xb, pz, ticket_url, login_url, initmy_url, buy_url, time_interval): self.driver_name = driver_name self.executable_path = executable_path self.username = username self.passwd = passwd self.starts = starts self.ends = ends self.dtime = dtime self.order = order self.userNames = userNames self.xb = xb self.pz = pz self.ticket_url = ticket_url self.login_url = login_url self.initmy_url = initmy_url self.buy_url = buy_url self.time_interval = time_interval self.setDriver() # set driver def setDriver(self): self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) self.driver.driver.set_window_size(1400, 1000) # 需要人工输入验证码 def login(self): conf.logger.info("登陆...") self.driver.visit(self.login_url) self.driver.fill("loginUserDTO.user_name", self.username) # sleep(1) self.driver.fill("userDTO.password", self.passwd) conf.logger.info("等待验证码,请人工自行输入...") while True: if self.driver.url != self.initmy_url: sleep(1) else: break # 选择乘客 def choice_passengers(self): flag_suc = False for user in self.userNames: user_choice = self.driver.find_by_css("#normal_passenger_id").find_by_text(user) if 1 <= len(user_choice): user_choice.last.click() conf.logger.info("乘车人%s选择成功.", user.encode('utf-8')) flag_suc = True else: conf.logger.warn("乘车人%s不存在, 请确认乘车人信息是否正确.", user.encode('utf-8')) if not flag_suc: raise "乘车人信息有误,请检查." # 选择座位类型 返回 True:选择成功 Flase:选择失败 def choice_seat(self): flag_choice = False # 点击选座按钮 cnt_passagers = len(self.userNames) if cnt_passagers < 1: raise "未配置乘车人." for cnt in range(1, cnt_passagers+1): # 点击座位类型 for seat_type in self.xb: seatId = "#seatType_" + str(cnt) self.driver.find_by_css(seatId).click() seat_butten = self.driver.find_by_css(seatId).find_by_value(seat_type) if seat_butten is None: conf.logger.info("座位类型%s售空",seat_type) continue if len(seat_butten) >= 1: seat_butten.click() flag_choice = True conf.logger.info("选择座位成功!") break return flag_choice # 增加cookies def add_cookies(self): # load cookies infomations self.driver.visit(self.ticket_url) self.driver.cookies.add({"_jc_save_fromStation": self.starts}) self.driver.cookies.add({"_jc_save_toStation": self.ends}) self.driver.cookies.add({"_jc_save_fromDate": self.dtime}) self.driver.reload() # 订票 def book_ticket(self): cnt = 0 while self.driver.url == self.ticket_url: # 查票 conf.logger.info("查票") try: self.driver.find_by_text(u"查询").click() warn_butten = self.driver.find_by_id("qd_closeDefaultWarningWindowDialog_id") if len(warn_butten) > 0: conf.logger.warn("日期不在预售期范围内") warn_butten.click() sleep(10) continue else: conf.logger.info("日期在预售期范围内,可以查票") book_Elementlist = self.driver.find_by_text(u"预订") if len(book_Elementlist) > 0: trains_indexs = [] selector = etree.HTML(self.driver.html) act_trainsNums = list(selector.xpath('//tr/td/div/div/div/a/text()')) for ord in self.order: try: index = act_trainsNums.index(ord) trains_indexs.append(index) except ValueError: conf.logger.warn("找不到车次%s", ord) pass if len(trains_indexs) > 0: for index in trains_indexs: book_Elementlist[index].click() except Exception: conf.logger.warn("查询失败,网络不稳定.") finally: sleep(self.time_interval) cnt = cnt + 1 conf.logger.info("没有可预订的票, 第%d次查询余票.", cnt) def run(self): self.login() conf.logger.info("登陆成功") while(1): try: # 开始抢票 while (1): try: # set cookies conf.logger.info("输入查票信息") self.add_cookies() # 预订 self.book_ticket() # 选择乘车人 self.choice_passengers() # 选座位等级 if(self.choice_seat()): break else: conf.logger.info("需要的座位类型已售空,重新查询余票") except Exception: conf.logger.info("程序有bug,请调试检查!") # 订单提交 self.driver.find_by_id('submitOrder_id').click() # 确认选座 self.driver.find_by_id('qr_submit_id').click() # 抢票成功 conf.logger.info("抢票成功,请在30分钟内去付款...") break except Exception: conf.logger.info("出票失败,重刷.")
time.sleep(5) #delay 5s before starting to search while count < maxCount: #perform the searches stub1 = searchFrom1[random.randrange(0, len(searchFrom1))] stub2 = searchFrom2[random.randrange(0, len(searchFrom2))] url = "http://www.bing.com/search?=setmkt=en-US&q=" url += '%s+%s' % (stub1, stub2) b.visit(url) time.sleep(1) count += 1 pwd = 'Bingbing' #quick hack for pwd change count = 0 time.sleep(5) #delay 5s before reporting b.visit('https://www.bing.com/rewards/dashboard') currentPointString = b.find_by_css("div.credits").first.value print 'Total credits for %s: %s' % (credentials[i], currentPointString) print 'With mobile: %d days.' % ((200 - int(currentPointString))/25 + 1) print 'Without mobile: %d days.' % ((200 - int(currentPointString))/15 + 1) b.quit() print '---------------------------------------------------------------' i += 1 print 'Script completed.'
DOWNLOAD_URL='http://www.miui.com/development.html' CONNECTIONS=10 OUTPUT_DIR='/tmp' LOG_DIR='/tmp' logger.info("Downloading roms from %s, each download with %d connections." % (DOWNLOAD_URL, CONNECTIONS)) logger.info("Output dir is '%s'." % OUTPUT_DIR) logger.info("Download Log dir is '%s'." % LOG_DIR) browser = Browser('firefox') logger.info('Loading the page.') browser.visit(DOWNLOAD_URL) logger.info('Page loaded.') # encontrando todos os links das roms (tag <a> com classe 'download') links = browser.find_by_css('a.download') # pegando nome do arquivo da url nomes_arquivos = [urlsplit(link['href']).path.split('/')[-1] for link in links] # pegando nome do aparelho no nome do arquivo nomes_aparelhos = [nome_arquivo.split('_')[1] for nome_arquivo in nomes_arquivos] # pegando a versão da MIUI versoes_miui = [nome_arquivo.split('_')[2] for nome_arquivo in nomes_arquivos] # pegando a versão do Android em que a MIUI foi feita versoes_android = [nome_arquivo.split('_')[-1][:-4] for nome_arquivo in nomes_arquivos] # montando um dicionario com os dados dados = zip(links, nomes_arquivos, nomes_aparelhos, versoes_miui, versoes_android) for (link, nome_arquivo, nome_aparelho, versao_miui, versao_android) in dados: if not Rom.existe(nome_aparelho): logger.debug('Device %s not found in database, adding it.' % nome_aparelho) rom = Rom.cria(nome_aparelho, versao_miui, versao_android, link['href'], nome_arquivo)
class AutoTicketsBot(object): def __init__(self, config): self.userConfig = config['Config'] self.browserConfig = config['Browser'] self.username = self.userConfig['username'] self.password = self.userConfig['password'] self.homeUrl = self.userConfig['homeUrl'] self.ticketPage = self.userConfig['ticketPage'] self.driver_name = 'chrome' self.executablePath = self.userConfig['executablePath'] self.ticketCount = self.userConfig['ticketCount'] def initBrowser(self): self.driver = Browser(driver_name=self.driver_name, executable_path=self.executablePath) self.driver.driver.set_window_size(self.browserConfig['width'], self.browserConfig['height']) def visitHomePage(self): self.driver.visit(self.homeUrl) if self.driver.is_element_present_by_text('English', wait_time=5) is True: self.driver.find_by_text('English').first.click() else: raise RuntimeError("Failed to switch language to English in home page...") def signInHomePage(self): if self.driver.is_element_present_by_text('Sign In', wait_time=5) is True: self.driver.find_by_text('Sign In').first.click() else: raise RuntimeError("Failed to find SignIn button in home page...") if self.driver.is_element_present_by_id('user_login', wait_time=2) is True: self.driver.find_by_id('user_login').fill(self.username) else: raise RuntimeError("Failed to fill account in signIn page...") if self.driver.is_element_present_by_id('user_password', wait_time=2) is True: self.driver.find_by_id('user_password').fill(self.password) else: raise RuntimeError("Failed to fill password in signIn page...") if self.driver.is_element_present_by_name('commit', wait_time=2) is True: self.driver.find_by_name('commit').first.click() else: raise RuntimeError("Failed to find SignIn button in signIn page...") def signInChecker(self, wait_time=3): if self.driver.is_element_not_present_by_text(self.username, wait_time=wait_time) is True: raise RuntimeError("Failed to sign in to the website...") def enterTicketPage(self): self.driver.visit(self.ticketPage) while self.driver.is_element_not_present_by_css('button[class="btn-default plus"]', wait_time=5) is True: self.driver.visit(self.ticketPage) def selectTicket(self): for __ in range(self.ticketCount): self.driver.find_by_css('button[class="btn-default plus"]')[-2].click() if self.driver.is_element_present_by_id('person_agree_terms', wait_time=2) is True: self.driver.find_by_id('person_agree_terms').check() else: raise RuntimeError("Failed to find agree_term checkbox in ticket page...") #self.captchaSolver("T2") if self.driver.is_element_present_by_text('Next Step', wait_time=2) is True: self.driver.find_by_text('Next Step').first.click() else: raise RuntimeError("Failed to find Next_Step button in ticket page...") def captchaSolver(self, answer): if self.driver.is_element_present_by_name('captcha_answer', wait_time=2) is True: self.driver.find_by_name('captcha_answer').fill('answer') else: raise RuntimeError("Failed to find Captcha in ticket page...") def quit(self): self.driver.quit()
# -*- coding: utf-8 -*- from splinter.browser import Browser import time # executable_path = {'executable_path': r'D:\webDriver\chromedriver.exe'} # b = Browser('chrome', **executable_path) b = Browser('firefox') b.visit('https://www.manhuaren.com/m76571/') time.sleep(30) print(b.html) # cs = b.cookies.all() cs = b.find_elements_by_class_name("TopstoryItem--experimentExpand") contentDivs = b.find_by_css("TopstoryItem--experimentExpand") print(len(contentDivs)) print(len(cs))
""" This example shows how to test web interface with splinter API with mouse interactions. In this page (http://douglasmiranda.com/labs/projetos/) when the mouse is over an element, it shows a description of itself. So let's automate the test to verify if this is ok. PS: This is a mouse interaction, so when you execute this script, you should stop moving the mouse :) """ from splinter.browser import Browser browser = Browser("chrome") browser.visit("http://douglasmiranda.com/labs/projetos/") li = browser.find_by_css("#ultimos-projetos li").first li.mouse_over() print "When the mouse is over the description is visible? ", li.find_by_css(".descricao").visible li.mouse_out() print "...and when the mouse is out the description is visible? ", li.find_by_css(".descricao").visible browser.quit()
class MyHandle: def __init__(self): self.browser = Browser("chrome") self.browser.visit("https://weibo.com/") time.sleep(10) def __del__(self): self.browser.quit() def setParam(self, m_title, m_para, m_html): self.m_title = m_title self.m_para = m_para self.m_html = m_html def setContent(self, content): self.content = content def setParser(self, parser): self.parser = parser def handleURL(self, url): media_weibo_cn = [['h2'], [ 'div[class="name m-text-cut"]', 'div[class="time"]' ], ['div[class="f-art"]']] weibo_com = [['div[class="title"]'], ['em[class="W_autocut"]', 'span[class="time"]'], ['div[class="WB_editor_iframe"]']] keywords = { 'div[class="m-feed"]': media_weibo_cn, 'div[class="main_editor "]': weibo_com } unvalid = [ 'div[class="B_unlog"]', 'div[class="WB_miniblog"]', 'div[class="tit"]', 'div[class="doc"]' ] self.browser.visit(url) time.sleep(1) now = datetime.now() rs = True while 'find_flag' not in vars(): for key, value in keywords.items(): content = self.browser.find_by_css(key) if content: find_flag = True for v in unvalid: if self.browser.find_by_css(v): print("Missing a page") find_flag = True rs = False if (datetime.now() - now).seconds > 60: print("Timeout a page") find_flag = True rs = False else: time.sleep(1) print("Sleep a while") for key, value in keywords.items(): content = self.browser.find_by_css(key) if content: self.setContent(content[0]) self.setParam(value[0], value[1], value[2]) self.handle() else: print("Can't find content!", key) return rs def handle(self): if self.content and isinstance( self.content, splinter.driver.webdriver.WebDriverElement): if self.m_title and isinstance(self.m_title, list): for ti in self.m_title: ti_content = self.content.find_by_css(ti) #print(ti_content.text) self.parser.add_heading(ti_content.text) if self.m_para and isinstance(self.m_para, list): for pa in self.m_para: pa_content = self.content.find_by_css(pa) #print(pa_content.text) self.parser.add_paragraph(pa_content.text) if self.m_html and isinstance(self.m_html, list): for ht in self.m_html: ht_content = self.content.find_by_css(ht) self.parser.feed(ht_content.html) else: print("Wrong content!")
""" interact with forms, send data, get data in html In this example we will: - visit the google's web page - do a search with the terms: the answer to life the universe and everything - get the specific data using the css selectors NOTE: Using the webdriver.firefox there a strange behavior, for some reason is used the suggestion of google in the search, that is, i want to search: "the answer to life the universe and everything" and because of the suggestion is auto choosen: "the answer to life the universe and everything else" I mean, for me is happening this... """ from splinter.browser import Browser browser = Browser('chrome') browser.visit('http://google.com') # Note: 'q' is the value of the atribute 'name', not the 'id' # <input type='text' name='q'... browser.fill('q', 'the answer to life the universe and everything') # find the submit buttom by the class atribute and click it browser.find_by_css('.lsb').first.click() # Note: find_by_css find elements in html using css selectors # like we use in a css file print browser.find_by_css('#topstuff .std h2').first.value browser.quit()
# -*- coding: utf-8 -*- """ Created on Wed Jul 29 02:27:11 2015 @author: bam8 """ # tasks for tomorrow # just start with one traders profile, doesn't matter who # and extract data from main page such as last seen and rank # then go into transactions page and get all of them from splinter.browser import Browser browser = Browser() browser.visit('http://google.com') browser.fill('q', 'splinter - python acceptance testing for web applications') button = browser.find_by_css('.lsb').first button.first.click() if browser.is_text_present('http://splinter.cobrateam.info'): print "Yes, found it! :)" else: print "No, didn't find it :(" browser.quit()
WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' #stickerList = open('msnSticker.txt') #open the the text file #allStickers = stickerList.readlines() #read all #resultFile = open('incomeResult.txt', "w") #report result eachSticker = 'T' print 'Income Statement of %s' % eachSticker b = Browser('chrome') #create a browser instance loginURL = 'http://investing.money.msn.com/investments/stock-income-statement/?symbol=%s' % eachSticker b.visit(loginURL) #login URL all_year = b.find_by_css("tr.mnrow1").find_by_tag("td") year = '' for each_year in all_year: if each_year.value != "": year += '%s\t' % each_year.value print year mnfh = b.find_by_css("tr.mnfh") mnfh_bold = b.find_by_css("tr.mnboldtopline") mnfh_three = b.find_by_css("tr.mnborderbottop3line") want = [6, 7, 8, 10, 11, 12, 14, 15, 17, 21] for i in range(len(mnfh)): if not i in want: continue mnfh_row = mnfh[i].find_by_tag("td")
# scroll up to load previous messages # i have not figured out a way to detect when the channel is completely loaded # mostly because i am lazy and it doesnt really matter # so i just put 300 as a safe number # if it's too low, increase it. if it's too high (takes too long) decrease it # in any case it only takes 15 minutes to do 300 scrolls for i in range(300): browser.execute_script( "document.querySelector('.messages-box .wrapper').scrollTo(0, -document.querySelector('.messages-box .wrapper').scrollHeight);" ) # for scroll loading time because rocket chat is a slow ass piece of shit time.sleep(3) # scrape raw file wrapper = browser.find_by_css('.messages-box .wrapper') html = BeautifulSoup(wrapper.html, 'html.parser') # use .replace('\\n', ' ').replace('\\t', '').replace('\\', '') to parse this rawfile.write(str(html.text.encode('unicode-escape'))) # scrape parsed data data = [] wrapper = browser.find_by_css('.messages-box .wrapper .message') i = 0 while True: try: thisdata = wrapper[i] thisdataparsed = [] thisdataparsed.append( thisdata._element.get_attribute('data-date').strip()) thisdatatextsplit = thisdata.text.split('\n')
WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' stickerList = open('msnSticker.txt') #open the the text file allStickers = stickerList.readlines() #read all resultFile = open('incomeResult.txt', "w") #report result for eachSticker in allStickers: print 'Income Statement of %s' % eachSticker, print '--------------------------------------------------------------------------------------------------' b = Browser('chrome') #create a browser instance loginURL = 'http://investing.money.msn.com/investments/stock-income-statement/?symbol=%s' % eachSticker b.visit(loginURL) #login URL year = b.find_by_css("tr.mnrow1") #find year allBold = b.find_by_css("tr.mnboldtopline") #find all bold lines netIncome = b.find_by_css("tr.mnborderbottop3line") #net income yearRaw = year.value #print year yearRaw = re.sub(' ', "\t\t", yearRaw) print "Year:\t\t\t%s" % yearRaw totalRevRaw = allBold[0].value #print total revenue totalRevArray = totalRevRaw.split("\n") totalRevArray[1] = re.sub(' ', "\t", totalRevArray[1]) print "Total Revenue:\t\t%s" % totalRevArray[1] grossProfitRaw = allBold[1].value #print gross profit grossProfitArray = grossProfitRaw.split("\n") grossProfitArray[1] = re.sub(' ', "\t", grossProfitArray[1])
browser = Browser() #As of March 27, 2016 inp = csv.reader(file(fd + 'Complete_list.csv', 'rb')) head = inp.next() for e, i in enumerate(head): print e, i fd2 = 'g:/health_data/provider_urls/' for i in inp: if not re.search("^None|NOT SUBMITTED", i[2]): print i[1] try: outp = csv.writer(open(os.path.join(fd2, i[1] + '.csv'), 'wb'), delimiter='\t') browser.visit(i[2]) sleep(1) try: need = browser.find_by_css('pre') proc = json.loads(need[0].text) except: need = browser.find_by_tag('body') proc = json.loads(re.sub('}.*?$', '}', need[0].text)) for p in proc['provider_urls']: outp.writerow([p]) #call('taskkill /F /IM firefox.exe') except: traceback.print_exc()
FAIL = '\033[91m' ENDC = '\033[0m' #stickerList = open('msnSticker.txt') #open the the text file #allStickers = stickerList.readlines() #read all #resultFile = open('incomeResult.txt', "w") #report result eachSticker = 'T' print 'Income Statement of %s' % eachSticker b = Browser('chrome') #create a browser instance loginURL = 'http://investing.money.msn.com/investments/stock-income-statement/?symbol=%s' % eachSticker b.visit(loginURL) #login URL all_year = b.find_by_css("tr.mnrow1").find_by_tag("td") year = '' for each_year in all_year: if each_year.value != "": year += '%s\t' % each_year.value print year mnfh = b.find_by_css("tr.mnfh") mnfh_bold = b.find_by_css("tr.mnboldtopline") mnfh_three = b.find_by_css("tr.mnborderbottop3line") want = [6, 7, 8, 10, 11, 12, 14, 15, 17, 21] for i in range(len(mnfh)): if not i in want: continue mnfh_row = mnfh[i].find_by_tag("td")
br.set_cookiejar(cj) browser = Browser() #As of March 27, 2016 inp = csv.reader(file(fd+'Complete_list.csv','rb')) head = inp.next() for e,i in enumerate(head): print e,i fd2 = 'g:/health_data/provider_urls/' for i in inp: if not re.search("^None|NOT SUBMITTED",i[2]): print i[1] try: outp = csv.writer(open(os.path.join(fd2,i[1]+'.csv'),'wb'),delimiter='\t') browser.visit(i[2]) sleep(1) try: need = browser.find_by_css('pre') proc = json.loads(need[0].text) except: need = browser.find_by_tag('body') proc = json.loads(re.sub('}.*?$','}',need[0].text)) for p in proc['provider_urls']: outp.writerow([p]) #call('taskkill /F /IM firefox.exe') except: traceback.print_exc()
class BrushTicket(object): """买票类及实现方法""" def __init__(self, passengers, from_time, from_station, to_station, numbers, seat_type, receiver_mobile, receiver_email): """定义实例属性,初始化""" # 乘客姓名 self.passengers = passengers # 起始站和终点站 self.from_station = from_station self.to_station = to_station # 车次 self.numbers = list(map(lambda number: number.capitalize(), numbers)) # 乘车日期 self.from_time = from_time # 座位类型所在td位置 if seat_type == '商务座特等座': seat_type_index = 1 seat_type_value = 9 elif seat_type == '一等座': seat_type_index = 2 seat_type_value = 'M' elif seat_type == '二等座': seat_type_index = 3 seat_type_value = 0 elif seat_type == '高级软卧': seat_type_index = 4 seat_type_value = 6 elif seat_type == '软卧': seat_type_index = 5 seat_type_value = 4 elif seat_type == '动卧': seat_type_index = 6 seat_type_value = 'F' elif seat_type == '硬卧': seat_type_index = 7 seat_type_value = 3 elif seat_type == '软座': seat_type_index = 8 seat_type_value = 2 elif seat_type == '硬座': seat_type_index = 9 seat_type_value = 1 elif seat_type == '无座': seat_type_index = 10 seat_type_value = 1 elif seat_type == '其他': seat_type_index = 11 seat_type_value = 1 else: seat_type_index = 7 seat_type_value = 3 self.seat_type_index = seat_type_index self.seat_type_value = seat_type_value # 通知信息 self.receiver_mobile = receiver_mobile self.receiver_email = receiver_email # 新版12306官网主要页面网址 self.login_url = 'https://kyfw.12306.cn/otn/resources/login.html' self.init_my_url = 'https://kyfw.12306.cn/otn/view/index.html' self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc' # 浏览器驱动信息,驱动下载页:https://sites.google.com/a/chromium.org/chromedriver/downloads self.driver_name = 'chrome' self.driver = Browser(driver_name=self.driver_name) def do_login(self): """登录功能实现,手动识别验证码进行登录""" self.driver.visit(self.login_url) sleep(1) # 选择登陆方式登陆 print('请扫码登陆或者账号登陆……') while True: if self.driver.url != self.init_my_url: sleep(1) else: break def start_brush(self): """买票功能实现""" # 浏览器窗口最大化 self.driver.driver.maximize_window() # 登陆 self.do_login() # 跳转到抢票页面 self.driver.visit(self.ticket_url) sleep(1) try: print('开始刷票……') # 加载车票查询信息 self.driver.cookies.add( {"_jc_save_fromStation": self.from_station}) self.driver.cookies.add({"_jc_save_toStation": self.to_station}) self.driver.cookies.add({"_jc_save_fromDate": self.from_time}) self.driver.reload() count = 0 while self.driver.url == self.ticket_url: self.driver.find_by_text('查询').click() sleep(1) count += 1 print('第%d次点击查询……' % count) try: start_list = self.driver.find_by_css('.start-t') for start_time in start_list: current_time = start_time.text current_tr = start_time.find_by_xpath('ancestor::tr') if current_tr: car_no = current_tr.find_by_css('.number').text if car_no in self.numbers: if current_tr.find_by_tag('td')[ self.seat_type_index].text == '--': print( '%s无此座位类型出售,继续……' % (car_no + '(' + current_time + ')', )) sleep(0.2) elif current_tr.find_by_tag('td')[ self.seat_type_index].text == '无': print( '%s无票,继续尝试……' % (car_no + '(' + current_time + ')', )) sleep(0.2) else: # 有票,尝试预订 print(car_no + '(' + current_time + ')刷到票了(余票数:' + str( current_tr.find_by_tag('td')[ self.seat_type_index].text) + '),开始尝试预订……') current_tr.find_by_css( 'td.no-br>a')[0].click() sleep(1) key_value = 1 for p in self.passengers: if '()' in p: p = p[:-1] + '学生' + p[-1:] # 选择用户 print('开始选择用户……') self.driver.find_by_text( p).last.click() # 选择座位类型 print('开始选择席别……') if self.seat_type_value != 0: self.driver.find_by_xpath( "//select[@id='seatType_" + str(key_value) + "']/option[@value='" + str(self.seat_type_value) + "']").first.click() key_value += 1 sleep(0.2) if p[-1] == ')': self.driver.find_by_id( 'dialog_xsertcj_ok').click() print('正在提交订单……') self.driver.find_by_id( 'submitOrder_id').click() sleep(2) # 查看放回结果是否正常 submit_false_info = self.driver.find_by_id( 'orderResultInfo_id')[0].text if submit_false_info != '': print(submit_false_info) self.driver.find_by_id( 'qr_closeTranforDialog_id').click( ) sleep(0.2) self.driver.find_by_id( 'preStep_id').click() sleep(0.3) continue print('正在确认订单……') self.driver.find_by_id( 'qr_submit_id').click() print('预订成功,请及时前往支付……') # 发送通知信息 self.send_mail(self.receiver_email, '恭喜您,抢到票了,请及时前往12306支付订单!') self.send_sms( self.receiver_mobile, '您的验证码是:8888。请不要把验证码泄露给其他人。') sys.exit(0) else: print('当前车次异常') except Exception as error_info: print(error_info) except Exception as error_info: print(error_info) def send_sms(self, mobile, sms_info): """发送手机通知短信,用的是-互亿无线-的测试短信""" host = "106.ihuyi.com" sms_send_uri = "/webservice/sms.php?method=Submit" account = "C59782899" pass_word = "19d4d9c0796532c7328e8b82e2812655" params = parse.urlencode({ 'account': account, 'password': pass_word, 'content': sms_info, 'mobile': mobile, 'format': 'json' }) headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain" } conn = httplib2.HTTPConnectionWithTimeout(host, port=80, timeout=30) conn.request("POST", sms_send_uri, params, headers) response = conn.getresponse() response_str = response.read() conn.close() return response_str def send_mail(self, receiver_address, content): """发送邮件通知""" # 连接邮箱服务器信息 host = 'smtp.163.com' port = 25 sender = '*****@*****.**' # 你的发件邮箱号码 pwd = 'FatBoy666' # 不是登陆密码,是客户端授权密码 # 发件信息 receiver = receiver_address body = '<h2>温馨提醒:</h2><p>' + content + '</p>' msg = MIMEText(body, 'html', _charset="utf-8") msg['subject'] = '抢票成功通知!' msg['from'] = sender msg['to'] = receiver s = smtplib.SMTP(host, port) # 开始登陆邮箱,并发送邮件 s.login(sender, pwd) s.sendmail(sender, receiver, msg.as_string())
url_using = l row = [] mainrow = [] try : awardnum = re.search('^\d+',l).group() except : awardnum = re.search('^\d+',l) log = open(path+'/browser_problems_v2.txt','a') try: browser = Browser() # browser.visit(url+l) browser.visit(l) #Project title title = browser.find_by_css('#title b') for t in title: row.append(t.text.encode('utf8')) mainrow.append('Project Title') #Basic information on award basicinfo = browser.find_by_css('.second') for b in basicinfo: # print b.text one = b.find_by_css('div') for n in range(1,len(one)-5,5): #you can also use .col tags here # row.extend([str888.sub('',one[n].text.encode('utf8')),str888.sub('',one[n+2].text.encode('utf8'))]) # mainrow.extend([str888.sub('',one[n+1].text.encode('utf8')),str888.sub('',one[n+3].text.encode('utf8'))]) row.extend([str888.sub('',one[n].text),str888.sub('',one[n+2].text)]) mainrow.extend([str888.sub('',one[n-1].text),str888.sub('',one[n+1].text)])