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 __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
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 browser_can_change_user_agent(self, webdriver): from splinter.browser import Browser browser = Browser(driver_name=webdriver, user_agent="iphone") browser.visit(EXAMPLE_APP + "useragent") result = 'iphone' in browser.html browser.quit() return result
def test_webdriverchrome_should_be_deprecated(self): with warnings.catch_warnings(record=True) as warnings_list: warnings.simplefilter('default') from splinter.browser import Browser browser = Browser('webdriver.chrome') browser.quit() warning_message = warnings_list[0].message.args[0] assert_equals("'webdriver.chrome' is deprecated, use just 'chrome'", warning_message)
def main(): br = Browser('chrome') br.visit(URL) sleep(3) if br.is_text_present("Remember me", wait_time=7): br.fill('username', NAME) br.fill('password', PASSWORD) br.execute_script('javascript:savesettings()')
def main(): browser = Browser() browser.visit(url) f = open("/tmp/source.html", "w") # save the source in a file print >>f, browser.html f.close() browser.quit() print '__END__'
def login(url, brower_name): """log in""" print "Executing login" browser = Browser(driver_name=brower_name) while datetime.now().second < 50: print datetime.now().second browser.visit(url) button = browser.find_by_id("ContentPlaceHolder1_hlOfferAlertAA") if button: button.click() else: print "not found"
def get_fuel(preco_alcool, preco_gasolina, consumo_alcool, consumo_gasolina, kilometragem, tanque): "Faz o srap do resultado do simplex no phpsimplex.com" browser = Browser("zope.testbrowser") URL = """http://www.phpsimplex.com/simplex/page2.php?objetivo=min&Submit=Continuar&restricciones=3&variables=2&l=es&x1=%(preco_alcool)f&x2=%(preco_gasolina)f&r11=%(consumo_alcool)f&r12=%(consumo_gasolina)f&desigualdad1=1&y1=%(kilometragem)f&r21=1&r22=1&desigualdad2=-1&y2=%(tanque)f&r31=-0.2&r32=1&desigualdad3=1&y3=0""" print URL % locals() browser.visit(URL % locals()) browser.click_link_by_partial_href("solucion2.php") html = BeautifulSoup(browser.html) text = "" try: text = html.findAll("p")[2].getText() # rgx = re.compile('\\d+\\.*\\d*') # rgx = re.compile('\\d+(?:\\.\\d*)*') # results = rgx.findall(text) if text.count("X1 = 0"): print "X1=0" # rgx = re.compile('\\d+\\.\\d+') rgx = re.compile("\\d+\\.*\\d*") results = rgx.findall(text) return {"custo_total": Decimal(results[0]), "vol_alcool": 0, "vol_gasolina": Decimal(results[1])} elif text.count("X2 = 0"): print "X2=0" rgx = re.compile("\\d+\\.\\d+") results = rgx.findall(text) return {"custo_total": Decimal(results[0]), "vol_alcool": Decimal(results[1]), "vol_gasolina": 0} else: rgx = re.compile("\\d+\\.\\d+") results = rgx.findall(text) return { "custo_total": Decimal(results[0]), "vol_alcool": Decimal(results[1]), "vol_gasolina": Decimal(results[2]), } except IndexError: vol_alcool = 0 vol_gasolina = kilometragem / consumo_gasolina custo_total = vol_gasolina * Decimal(preco_gasolina) print "fuuuu" return { "custo_total": Decimal(custo_total), "vol_alcool": Decimal(vol_alcool), "vol_gasolina": Decimal(vol_gasolina), }
def splinter(brow,url,id,pwd): browser = Browser(brow) #login 126 email websize browser.visit(url) #wait web element loading time.sleep(5) #fill in account and password browser.find_by_id('idInput').fill(id) browser.find_by_id('pwdInput').fill(pwd) #click the button of login browser.find_by_id('loginBtn').click()
def load_tests(loader, tests, ignore): global browser browser = Browser("chrome") browser.wait_time = 1 for test_path in filter(lambda path: path.endswith(".rst"), os.listdir("docs")): tests.addTests(doctest.DocFileSuite(os.path.join("docs", test_path), globs={ "rajesh": rajesh, "browser": browser, "start_rajesh": start_rajesh, "stop_rajesh": stop_rajesh, }, ) ) return tests
def form_fill(user, form): display = Display(visible=0, size=(800, 600)) display.start() if form.email != {}: st = emailFill(user, form) email_send("Software Internship Application",st,[form.email['email'], user.getField(2)],attachment=user.getField(6)) return browser = Browser() browser.visit(form.url) if form.greenHouseResume: string = """$("#application_form").append('<input type="hidden" id="resume_url" name="job_application[resume_url]" value=""" + '"' + user.getField(6.1) + '"' + """><input type="hidden" id="resume_url_filename" name="job_application[resume_url_filename]" value="resume.pdf">')""" browser.execute_script(string) def doClicks(): #partial_htmls being dealt with for ele in form.html_button_clicks: browser.find_link_by_partial_href(ele)[0].click() def fillField(): for ele, tple in form.Fill_Fields.iteritems(): browser.fill(ele,user.getField(tple[0])) def attachField(): for ele, field in form.Attach_Fields.iteritems(): browser.attach_file(ele,os.getcwd() + "/" + user.getField(field)) def dropDownField(): for ele, index in form.Drop_Down_Fields.iteritems(): browser.execute_script('document.getElementById("' + ele + '")[' + str(index) + '].selected=true') def dropDownHelper(): for ele,index in form.Drop_Down_Fields_Helper.iteritems(): browser.execute_script('document.getElementsByTagName("'+ele+'")['+str(index)+'].selected=true') def checkBoxes(): for ele in form.CheckBoxes: browser.execute_script('document.getElementById("' + ele + '").checked=true') def doJS(): for ele in form.extraJavaScript: browser.execute_script(ele) def doJSHelp(): for ele, index in form.extraJSHelper.iteritems(): browser.execute_script('document.getElementById("' + ele +'").value = "'+ user.getField(index) +'";') def submitJS(): for ele in form.submitJS: browser.execute_script(ele) doClicks() fillField() attachField() dropDownField() dropDownHelper() checkBoxes() doJS() doJSHelp() submitJS() if not form.submitJS: browser.execute_script('document.getElementsByClassName("' + form.finalSubmit + '")[document.getElementsByClassName("' + form.finalSubmit + '").length-1].click()') time.sleep(10) browser.quit() display.stop()
def test_adding_a_keypair(self): self.browser = Browser() self._logIn() keypair = self.browser.find_link_by_text("Keypairs") keypair.click() self.lookForError() keypair_btn = self.browser.find_by_id("keypairs_create_link") keypair_btn.click() self.browser.fill('name', "test_keypair_test") # Create Keypair in d5 button = self.browser.find_by_value('Add Keypair') button.click() return_link = self.browser.find_link_by_partial_text("Return to keypairs") return_link.click() self.assertTrue(self.browser.is_text_present("test_keypair_test"), "Created keypair not found") delete_link = self.browser.find_by_id("delete_test_keypair_test") delete_link.click() # accept the js alert alert = self.browser.get_alert() alert.accept() # verify delete message self.assertTrue(self.browser.is_text_present( "Successfully deleted keypair: test_keypair_test"), "Keypair not successfully deleted") self._logOut() self.browser.quit()
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 main(): #browser = Browser('zope.testbrowser') #browser = Browser('webdriver.chrome') browser = Browser() browser.visit(url) #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;") while 'ORIGIN' not in browser.html: sleep(5) f = open("/tmp/source.html", "w") print >>f, browser.html f.close() browser.quit() print '__END__'
def __init__(self, width = 500, height = 300): """ 抖音App视频下载 """ # 无头浏览器 chrome_options = Options() chrome_options.add_argument('user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"') self.driver = Browser(driver_name='chrome', executable_path='D:/chromedriver', options=chrome_options, headless=True)
def test_instances_page(self): self.browser = Browser() self._logIn() instances = self.browser.find_link_by_text("Instances") instances.click() self.lookForError() self._logOut() self.browser.quit()
def setUp(self, from_bdd=False): if from_bdd: if isinstance( self, BDDTestCase ): browser_name = getattr(settings,'BDD_BROWSER','firefox') self.browser = Browser(browser_name) self.extra_setup() else: self.prepare_database()
def test_forward_to_none_page(self): "should not fail when trying to forward to none" browser = Browser('zope.testbrowser') browser.visit(EXAMPLE_APP) browser.forward() assert_equals(EXAMPLE_APP, browser.url) browser.quit()
def downloadCSV( geneName ): ## import webdriver from splinter.browser import Browser ## alternative driver api ## driver = webdriver.Chrome("C:/Users/Jeff/Downloads/chromedriver.exe") ## import the driver, and visit the exac.broadinstitute.org webpage ## b = driver.get("http://exac.broadinstitute.org/awesome?query=SAMD11") ## load chrome driver, visit targer url b = Browser(driver_name="chrome") url = "http://exac.broadinstitute.org/awesome?query=" + geneName b.visit(url) ## find the LoF button, by web elementary inspect button = b.find_by_id(u"consequence_lof_variant_button") ## trigger the button button.click() ## find the Export to CSV button, also by web elementary inspect button2 = b.find_by_id(u"export_to_csv") ## trigger the button, download the target CSV document button2.click() b.quit()
def test_overview_page(self): self.browser = Browser() self._logIn() overview = self.browser.find_link_by_text("Overview") overview.click() self.lookForError() self.validateOverview() self._logOut() self.browser.quit()
def main(): # browser = Browser('chrome') browser = Browser() browser.visit(PE_LOGIN) browser.fill('username', USERNAME) browser.fill('password', PASSWORD) button = browser.find_by_name('login') button.click() browser.visit(PE_COUNTRIES) f = open("/tmp/stat.html", "w") print >>f, browser.html f.close() browser.quit() print '__END__'
def test_create_a_shorten_url(self): browser = Browser() browser.visit('http://localhost:8000/') browser.fill('long_url', 'google.com') button = browser.find_by_id('btShorten') button.click() short_url = browser.find_by_id('short_url') self.assertEqual('http://localhost:8000/1', short_url.value)
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()
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)
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 start(self): # 初始化驱动 self.driver=Browser(driver_name=self.driver_name,executable_path=self.executable_path) # 初始化浏览器窗口大小 self.driver.driver.set_window_size(1400, 1000) # 登录,自动填充用户名、密码,自旋等待输入验证码,输入完验证码,点登录后,访问 tick_url(余票查询页面) self.login() # 登录成功,访问余票查询页面 self.driver.visit(self.ticket_url) # 自动购买车票 self.buyTickets();
def __init__ ( self ): #self.base_url = "http://www.webcamps.com.br/" #self.base_url = "http://localhost" self.base_url = "http://www.scarpan.com.br" self.links = [] self.visited = ["%s/" % self.base_url] browsers = ["webdriver.firefox", "webdriver.chrome", "zope.testbrowser"] self.browser = Browser(browsers[randint(0,2)]) #Redirect Messages firefox.redirect_error("/tmp/twill.err") firefox.redirect_output("/tmp/twill.out") firefox.go( self.base_url ) self.browser.visit( self.base_url ) self.extractAllLinks ( firefox.showlinks() ) self.visitAllLinks()
def search_click(): browser = Browser('firefox', profile='fraudbot') # Visit URL url = "http://www.google.com" browser.visit(url) browser.fill('q', "karmachamillionaire") time.sleep(1) # Find and click the 'search' button button = browser.find_by_name("btnG") # Interact with elements button.click() time.sleep(2) if browser.is_text_present("did not match any documents"): print "nobody likes us =(" else: print "we're popular =)" browser.quit()
def handle(self, *args, **options): browser = Browser('phantomjs') dims = (1600, 1000) browser.driver.set_window_size(dims[0], dims[1]) for project in ProjectPage.objects.all(): links = project.links.filter(public=True, type='main') if not links: continue # Use only the first link for now link = links[0] print("Visiting %s (%s)" % (link.url, link)) browser.visit(link.url) assert browser.status_code.is_success() time.sleep(5) with tempfile.NamedTemporaryFile(suffix='.png', prefix='project') as tmpf: browser.driver.save_screenshot(tmpf.name) pil_image = PILImage.open(tmpf) pil_image = pil_image.crop((0, 0, dims[0], dims[1])) tmpf.seek(0) tmpf.truncate(0) pil_image.save(tmpf, format='PNG') title = '%s screenshot' % project.title try: image = Image.objects.get(title=title) except Image.DoesNotExist: image = Image(title=title) image.file = ImageFile(tmpf) image.save() project.image = image project.save(update_fields=['image']) browser.quit()
def start_buy(self): self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) self.driver.driver.set_window_size(700, 500) self.login() self.driver.visit(self.ticket_url) try: self.title = '提示' self.message = '开始购票!' messagebox.showinfo(self.title, self.message) # 加载查询信息 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() if self.order != '0': while self.driver.url == self.ticket_url: self.driver.find_by_text('查询').click() # self.title = '提示' # self.message = '开始尝试预订...' # messagebox.showinfo(self.title, self.message) try: self.driver.find_by_text('预订')[self.order].click() sleep(1.5) except: # self.title = '提示' # self.message = '预订失败...' # messagebox.showinfo(self.title, self.message) continue else: while self.driver.url == self.ticket_url: self.driver.find_by_text('查询').click() # self.title = '提示' # self.message = '开始尝试预订...' # messagebox.showinfo(self.title, self.message) try: for i in self.driver.find_by_text('预订'): i.click() sleep(1) except: # self.title = '提示' # self.message = '预订失败...' # messagebox.showinfo(self.title, self.message) continue self.title = '提示' self.message = '开始选择用户~~~' messagebox.showinfo(self.title, self.message) sleep(1) for p in self.passengers: self.driver.find_by_text(p).last.click() sleep(0.5) if p[-1] == ')': self.driver.find_by_id('dialog_xsertcj_ok').click() self.title = '提示' self.message = '提交订单中...' messagebox.showinfo(self.title, self.message) sleep(1) self.driver.find_by_id('submitOrder_id').click() sleep(2) self.title = '提示' self.message = '确认选座中...' messagebox.showinfo(self.title, self.message) self.driver.find_by_id('qr_submit_id').click() self.title = '提示' self.message = '预订成功...' messagebox.showinfo(self.title, self.message) self.content = '恭喜您抢票成功,请在半小时内完成支付!!!' _ = os.system('mshta vbscript:createobject("sapi.spvoice").speak("%s")(window.close)' % self.content) except: self.title = '提示' self.message = '出现了一点小错误,可能是输入信息有误导致的...' messagebox.showinfo(self.title, self.message)
def testLogin(desc, username, password, result): """ fill login form message and submit, check result message and print """ output(desc) browser.fill('TPL_username', username.decode(UTF8)) browser.fill('TPL_password', password.decode(UTF8)) browser.find_by_value('登录').first.click() checkresult(result) __testUrl = 'http://waptest.taobao.com/login/login.htm?tpl_redirect_url=http%3A%2F%2Fm.taobao.com%2F' # chrome driver : http://code.google.com/p/selenium/wiki/ChromeDriver # already support firefox browser = Browser() browser.visit(__testUrl) output("测试页面:" + browser.title) try: # test login testLogin('测试未输入用户名', '', '', '请输入会员名') testLogin('测试未输入密码', 'qd_test_001', '', '请输入密码') testLogin('测试帐户不存在', '这是一个不存在的名字哦', 'xxxxxxx', '该账户名不存在') testLogin('测试成功登录', 'qd_test_001', 'taobao1234', '继续登录前操作') # test find password output("测试[找回密码]链接") browser.visit(__testUrl) backPasswordLink = browser.find_link_by_text('取回密码')
CLOASE_AFTER_TEST = False def resultMsg(x): assert x == True print 'pass' def testLogin(desc, username, password, result): print desc browser.fill('email', username) browser.fill('pass', password) browser.find_by_value('login').first.click() checkresult(result) def checkresult(x): resultMsg(browser.is_text_present(x)) __testUrl = 'http://ubuntu1604-006.student.cs.uwaterloo.ca:19489/' browser = Browser('chrome', headless=True) browser.visit(__testUrl) print 'test page:' + browser.title testLogin('no username', '', '', 'please enter your email') testLogin('no password', 'lyuboxin@gmail', '', 'please enter your password') testLogin('unexist username', 'lyuboxin1@gmail', '', 'username or password is wrong') if CLOASE_AFTER_TEST: browser.quit()
def initial_setup(server): """ Launch the browser once before executing the tests. """ world.absorb(settings.LETTUCE_SELENIUM_CLIENT, 'LETTUCE_SELENIUM_CLIENT') if world.LETTUCE_SELENIUM_CLIENT == 'local': browser_driver = getattr(settings, 'LETTUCE_BROWSER', 'chrome') if browser_driver == 'chrome': desired_capabilities = DesiredCapabilities.CHROME desired_capabilities['loggingPrefs'] = { 'browser': 'ALL', } elif browser_driver == 'firefox': desired_capabilities = DesiredCapabilities.FIREFOX else: desired_capabilities = {} # There is an issue with ChromeDriver2 r195627 on Ubuntu # in which we sometimes get an invalid browser session. # This is a work-around to ensure that we get a valid session. success = False num_attempts = 0 while (not success) and num_attempts < MAX_VALID_BROWSER_ATTEMPTS: # Load the browser and try to visit the main page # If the browser couldn't be reached or # the browser session is invalid, this will # raise a WebDriverException try: world.browser = Browser( browser_driver, desired_capabilities=desired_capabilities) world.browser.driver.set_script_timeout(GLOBAL_SCRIPT_TIMEOUT) world.visit('/') except WebDriverException: if hasattr(world, 'browser'): world.browser.quit() num_attempts += 1 else: success = True # If we were unable to get a valid session within the limit of attempts, # then we cannot run the tests. if not success: raise IOError( "Could not acquire valid {driver} browser session.".format( driver=browser_driver)) world.absorb(0, 'IMPLICIT_WAIT') world.browser.driver.set_window_size(1280, 1024) elif world.LETTUCE_SELENIUM_CLIENT == 'saucelabs': config = get_saucelabs_username_and_key() world.browser = Browser( 'remote', url="http://{}:{}@ondemand.saucelabs.com:80/wd/hub".format( config['username'], config['access-key']), **make_saucelabs_desired_capabilities()) world.absorb(30, 'IMPLICIT_WAIT') world.browser.set_script_timeout(GLOBAL_SCRIPT_TIMEOUT) elif world.LETTUCE_SELENIUM_CLIENT == 'grid': world.browser = Browser( 'remote', url=settings.SELENIUM_GRID.get('URL'), browser=settings.SELENIUM_GRID.get('BROWSER'), ) world.absorb(30, 'IMPLICIT_WAIT') world.browser.driver.set_script_timeout(GLOBAL_SCRIPT_TIMEOUT) else: raise Exception("Unknown selenium client '{}'".format( world.LETTUCE_SELENIUM_CLIENT)) world.browser.driver.implicitly_wait(world.IMPLICIT_WAIT) world.absorb(world.browser.driver.session_id, 'jobid')
from splinter.browser import Browser #from time import sleep b = Browser('firefox') b.visit('http://www.baidu.com') #b.fill('wd', 'test\r') #b.fill('wd', 'test\n') #b.fill('wd', 'test\r\n') b.fill('wd', 'test') #b.execute_script('document.getElementsByName("f")[0].submit()') script = ''' var e = document.createEvent('KeyboardEvent'); e.initKeyEvent('keypress', true, true, window, false, false, false, false, 13, 0); document.getElementsByName('wd')[0].dispatchEvent(e); ''' b.execute_script(script) #sleep(5) b.type('wd', ' python') #b.type('wd', ' python', slowly=True) # slowly not working
from splinter.browser import Browser b = Browser(driver_name="chrome") url = 'https://kyfw.12306.cn/otn/leftTicket/init' b = Browser(driver_name="chrome") b.visit(url) b.find_by_text(u"登录").click() b.fill("loginUserDTO.user_name","xxxx") b.fill("userDTO.password","xxxx") b.cookies.all() {u'BIGipServerotn': u'1977155850.38945.0000', u'JSESSIONID': u'0A01D97598F459F751C4AE8518DBFB300DA7001B67', u'__NRF': u'95D48FC2E0E15920BFB61C7A330FF2AE', u'_jc_save_fromDate': u'2018-02-22', u'_jc_save_fromStation': u'%e5%b0%9a%e5%bf%97', u'_jc_save_toStation': u'%e6%9d%ad%e5%b7%9e', u'current_captcha_type': u'Z'} b.cookies.add({"_jc_save_fromStation":"%e5%b0%9a%e5%bf%97"}) 添加出发日期 b.cookies.add({"_jc_save_fromDate":"2018-02-22"}) 添加目的地 b.cookies.add({u'_jc_save_toStation':'%e6%9d%ad%e5%b7%9e'}) b.reload() b.find_by_text(u"查询").click() autoSearchTime=1000
print len(file_list) for item in file_list: file_name = item.find_by_tag('td').first.text print file_name, '++++++++++++++++++' item.find_by_tag('td').first.click() file_path = os.path.join(browser_save_path, file_name) time.sleep(1) if os.path.exists(file_path): shutil.move(file_path, save_path) else: print file_name b.quit() if __name__ == '__main__': # browser_save_path = 'C:/Users/wt/Downloads' # base_path = 'D:/work/barra/' # login_url = 'https://fileservice.msci.com/' # base_url = 'https://fileservice.msci.com/m/home/hcqlztti/barra/' # path_list = ['cne5/', '/cne5/model_receipt/', '/cne5/daily/', '/bime/'] # download_by_website(login_url, path_list, base_url, browser_save_path, base_path) # shutil.rmtree('D:/work/download/') # shutil.move(browser_save_path, 'D:/work/barra/cne5/') b = Browser(driver_name='chrome') b.visit('https://vpn-guest.citicsinfo.com') b.fill('svpn_name', 'bj-dongsanhuan-1') b.fill('svpn_password', 'EQJtqXC2') b.find_by_text(u'登 录').click() time.sleep(2) b.find_by_id('sendSms').click()
def scrapePosts(b_index, user): global browser_available global browsers print("BROWSER IN USE: " + str(b_index) + " OF " + str(len(browser_available))) #print("BROWSERLEN " + str(len(browsers))) #browser = browsers[b_index] #browser = Browser('firefox') #browser.visit("http://gab.ai") #browser.driver.implicitly_wait(3) #username = "******" #password = "******" #browser.find_by_name("username").fill(username) #browser.find_by_name("password").fill(password) #print(browser.find_by_text("Login")[1].click()) browser = browsers[b_index] # reset if necessary browser_count[b_index] += 1 try: if browser_count[b_index] >= BROWSE_LIMIT: browser.quit() crawler = Browser('firefox', headless=True) crawler.visit("http://gab.ai") crawler.driver.implicitly_wait(3) username = "******" password = "******" crawler.find_by_name("username").fill(username) crawler.find_by_name("password").fill(password) crawler.find_by_text("Login")[1].click() browsers[b_index] = crawler browser_count[b_index] = 0 print("BROWSER {} RESET".format(b_index)) except: print("failed to reset browser") browser = browsers[b_index] browser.visit("http://gab.ai/" + user) browser.driver.implicitly_wait(3) try: soup = BeautifulSoup(browser.html, features="html.parser") except selenium.common.exceptions.TimeoutException: browser_available[b_index] = True return AttributeError i = 0 count = 0 exceptcounter = 0 try: load_count = 0 while browser.find_by_text("Load more ") and load_count <= LOAD_LIMIT: browser.driver.implicitly_wait(1) time.sleep(1) try: browser.find_by_css('.post-list__load-more').click() load_count += 1 soup = BeautifulSoup(browser.html, features="html.parser") except Exception: exceptcounter += 1 if exceptcounter == 2: break continue posts = soup.findAll("post") if len(posts) == count: break count = len(posts) except selenium.common.exceptions.WebDriverException: browser_available[b_index] = True return soup = BeautifulSoup(browser.html, features="html.parser") postlist = soup.findAll("post") for i, post in enumerate(postlist): try: ScrapAndAddPosts(post, user, b_index) except Exception as e: error = "FAILED TO PROCESS POST {} FOR USER {}: {}".format(i,user, e) print(error) errorlog.write(error) browser_available[b_index] = True
from bs4 import BeautifulSoup from splinter.browser import Browser import pandas as pd import requests import html5lib executable_path = {'executable_path': 'chromedriver'} browser = Browser('chrome', **executable_path, headless=False) def mars_news(browser): url = "https://mars.nasa.gov/news/" browser.visit(url) browser.is_element_present_by_css("ul.item_list li.slide", wait_time=0.5) html = browser.html news_soup = BeautifulSoup(html, "html.parser") try: slide_element = news_soup.select_one("ul.item_list li.slide") slide_element.find("div", class_="content_title") news_title = slide_element.find("div", class_="content_title").get_text() news_paragraph = slide_element.find( "div", class_="article_teaser_body").get_text() except AttributeError: return None, None
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 handle(self, driver, row, page, *args, **kwargs): browser = Browser(driver) browser.visit('http://www.dataescolabrasil.inep.gov.br/dataEscolaBrasil/') with browser.get_iframe(0) as iframe: iframe.check('situacaoEmAtividade') iframe.select('estadoDecorate:estadoSelect', '41') assert iframe.is_text_present('Abatia', wait_time=5) # wait a known city iframe.find_by_id('pesquisar').first.click() assert iframe.is_text_present('Foram encontradas', wait_time=5) if page > 1: next_page = 1 def click_pager(): scroller = iframe.find_by_id('resultadoDataScroller').first pager = scroller.find_by_xpath('//td[text()="{0}"]'.format(next_page)) pager.first.click() while 1: try: click_pager() except ElementDoesNotExist: try: print 'retry', next_page os.system('sleep 1s') click_pager() except ElementDoesNotExist: print 're-retry', next_page os.system('sleep 2s') click_pager() os.system('sleep 2s') if next_page == page: break next_page += 1 while 1: # Reload the trs to avoid outdated references trs = iframe.find_by_css('#resultado tbody tr') # Go to the next page when done if (row + 1) > len(trs): page += 1 row = 0 scroller = iframe.find_by_id('resultadoDataScroller').first pager = scroller.find_by_xpath('//td[text()="{0}"]'.format(page)) pager.first.click() continue # Click first td anchor and assert it is the detail tab tr = trs[row] tds = tr.find_by_css('td') tds[0].find_by_css('a').click() if not iframe.is_text_present('Dados gerais'): browser.back() row += 1 iframe.find_by_xpath('//td[text()="Resultado de busca"]').first.click() continue # Get tab content content = iframe.find_by_css('.rich-tabpanel-content') data = self.data_from_ids(content, { 'external_code': 'dad_codEntidadeDecorate:dad_codEntidade', 'name': 'dad_noEntidadeDecorate:dad_noEntidade', 'city': 'dad_noMunicipioDecorate:dad_noMunicipio', 'address': 'dad_enderecoDecorate:dad_endereco', 'number': 'dad_numeroDecorate:dad_numero', 'complement': 'dad_complementoDecorate:dad_complemento', 'quarter': 'dad_bairroDecorate:dad_bairro', 'postal_code': 'dad_cepDecorate:dad_cep', 'phones': 'dad_numTelefoneDecorate:dad_numTelefone', 'email': 'dad_emailDecorate:dad_email', 'cnpj': 'cnpjEpDecorate:cnpjEp', 'adm_structure': 'dad_dependenciaAdmDecorate:dad_dependenciaAdm', 'private_school_category': 'descCategoriaEscPrivadaDecorate:descCategoriaEscPrivada', }) try: self.save_data(data, row=row, page=page) except IntegrityError: pass # could not save because cnpj already exists # Go back and move to next row iframe.find_by_xpath('//td[text()="Resultado de busca"]').first.click() row += 1
def setUpClass(cls): translation.activate('pt-br') cls.browser = Browser(settings.SPLINTER_TEST_DRIVER) super(BaseTestCase, cls).setUpClass()
def huoche(): global b b = Browser(driver_name="chrome") b.visit(ticket_url) b.execute_script('alert("开始刷票喽~~~~")') sleep(2) b.get_alert().dismiss() while b.is_text_present(u"登录"): sleep(1) login() if b.url == initmy_url: break try: #跳回购票页面 b.visit(ticket_url) #加载查询信息 b.cookies.add({"_jc_save_fromStation": starts}) b.cookies.add({"_jc_save_toStation": ends}) b.cookies.add({"_jc_save_fromDate": dtime}) b.reload() i = 1 #循环点击预订 if order != 0: while b.url == ticket_url: b.find_by_text(u"查询").click() # b.find_by_text(ttype).click() if b.find_by_text(u"预订"): sleep(0.3) b.find_by_text(u"预订")[order - 1].click() print b.url if b.is_text_present(u"证件号码", wait_time=0.2): # print [ i.text for i in b.find_by_text(pa) ] b.find_by_text(pa)[1].click() else: b.execute_script('alert("似乎没有可预订选项")') b.get_alert().dismiss() pass else: while b.url == ticket_url: b.find_by_text(u"查询").click() if b.find_by_text(u"预订"): sleep(0.3) for i in b.find_by_text(u"预订"): i.click() sleep(0.1) if b.is_text_present(u"证件号码"): b.find_by_text(pa)[1].click() else: b.execute_script('alert("似乎没有可预订选项")') b.get_alert().dismiss() pass b.execute_script('alert("能做的都做了")') b.get_alert().dismiss() print u"能做的都做了.....不再对浏览器进行任何操作" except Exception: print u"出错了...."
def IsBot(user, follower, link): print("\nCHECKING IF {} IS A BOT".format(user)) global bot global nonbot global browsers global browser_available global antidoubler global browser global primary_count if IsKnownBot(user): print("USER {} IS A BOT".format(user)) return True elif user in nonbot or UserInDB(user): statement = ("INSERT INTO Gab.Follows(Follower, Followee)VALUES(%s,%s)") values = (user,link) if follower else (link, user) if not LinkInDB(values[0], values[1]): DBexecuter("insert", statement, values, cursor, cnx) return False else: print("DOING FIRST TIME PROCESSING FOR {}".format(user)) # check if we need to reset browser primary_count += 1 if primary_count >= BROWSE_LIMIT: browser.quit() crawler = Browser('firefox', headless=True) crawler.visit("http://gab.ai") crawler.driver.implicitly_wait(3) username = "******" password = "******" crawler.find_by_name("username").fill(username) crawler.find_by_name("password").fill(password) crawler.find_by_text("Login")[1].click() browser = crawler primary_count = 0 print("PRIMARY BROWSER RESET") # check if user is bot or not page = getHTML("https://gab.ai/" + user) soup = BeautifulSoup(page.html, features="html.parser") isBot = False try: # find bio bio = soup.find('div', {'class':'profile__bio'}) # bio message bioMessage = bio.find("div").text # find metadata profileBar = soup.find('div', {'class':'profile__bar'}) meta = profileBar.findAll('a') # number of posts numposts = int(meta[1].text.split()[0].replace(',', '')) # number of followers numfollowers = int(meta[2].text.split()[0].replace(',', '')) # number of following numfollowing = int(meta[3].text.split()[0].replace(',', '')) # perform bot check logic if numposts == 0: isBot = True elif float(numfollowers)/numposts > 100: isBot = True elif numfollowing != 0 and float(numfollowing)/numposts > 100: isBot = True elif bioMessage[0] == '"' or bioMessage[0] == "'": isBot = True elif len(bioMessage.split()) >= 3: bioList = bioMessage.split() if bioList[-2][0] == '-' or bioList[-3] == '-': isBot = True except: print("ERROR PARSING USER") isBot = True # update bot / nonbot if isBot: print("USER {} IS A BOT".format(user)) AddBot(user) #bot.add(user) else: nonbot.add(user) # if not bot, parse it here and now instead of reloading it # if is bot, push name into meta db if isBot: try: print("Adding {} to meta bot DB".format(user)) statement = """insert into bots (name) values("{}")""".format(user) metacursor.execute(statement) metacon.commit() except: print("Failed to add {} to meta bot DB".format(user)) else: # that is, not isBot print("ADDING NEW USER {} TO DATABASE AND SCRAPING POSTS".format(user)) if not UserInDB(user) and not user in antidoubler: try: antidoubler.add(user) AddUser(user) print("GETTING POSTS FOR USER " + user) #print("JUST CHECKING... LEN BROWSERS is {}".format(len(browsers))) # grab posts here # check for an available browser while True: found = False for i in range(browser_limit): #print("A - iteration " + str(i)) if browser_available[i] == True: found = True #print("B - browser available") browser_available[i] = False #print("C - set browser to unavailable") #p = Process(target=scrapePosts, args=(i, user)) p = threading.Thread(target=scrapePosts, args=(i, user)) #print("D - process set") threads.append(p) #print("E - appended thread to threads list") p.start() #print("F - started thread") print(user + " IS BEING CRAWLED ON BROWSER " + str(i)) break if found: print("DUPLICATE REQUEST IGNORED") break time.sleep(1) except: print("ENCOUNTERED EXCEPTION DOWNLOADING POSTS") return isBot if UserInDB(user): statement = ("INSERT INTO Gab.Follows(Follower, Followee)VALUES(%s,%s)") values = (user,link) if follower else (link, user) if not LinkInDB(values[0], values[1]): DBexecuter("insert", statement, values, cursor, cnx) return isBot
def download_by_website(login_url, path_list, base_url, browser_save_path, base_path): b = Browser(driver_name='chrome') b.visit(login_url) b.fill('username', 'hcqlztti') b.fill('password', 'S6rbiowsmhqit?') b.find_by_text('Sign In').click() for path in path_list: curr_url = base_url + path save_path = base_path + path b.visit(curr_url) file_list = b.find_by_id('row').first.find_by_tag( 'tbody').first.find_by_tag('tr') print len(file_list) for item in file_list: file_name = item.find_by_tag('td').first.text print file_name, '++++++++++++++++++' item.find_by_tag('td').first.click() file_path = os.path.join(browser_save_path, file_name) time.sleep(1) if os.path.exists(file_path): shutil.move(file_path, save_path) else: print file_name b.quit()
def MakeNetwork(): global bot global nonbot #global seen global primary_count global browser # cheat a little... cut the bot checking wait... global metacursor global metacon config = { 'user': "******", 'password': '******', 'host': 'localhost', 'database': 'Meta', 'raise_on_warnings': True, 'charset': 'utf8mb4', 'collation': 'utf8mb4_bin' } metacon = mysql.connector.connect(**config) metacursor = metacon.cursor(buffered=True) readBots = "Select name from bots" botusers = DBexecuter("select", readBots, None, metacursor, metacon) for b in botusers: botuser = b[0].decode("utf-8") #seen.add(botuser) MakeSeen(botuser) AddBot(botuser) #bot.add(botuser) print("Added to botlist from DB: {}".format(botuser)) #readUsers = "Select User.username from User left join Follows on User.username = Follows.followee where Follows.Follower is null order by User.followers desc" readUsers = "Select username from User" #readUsers = "Select `User`.`username` From User Order By User.Followers DESC" #readUsers = "Select A.Followee, (`User`.`followers` - A.counts) as Dif from (select Followee, count(*) as counts from Follows Group by Follows.Followee) as A join User on User.username = A.Followee order by Dif DESC" users = DBexecuter("select", readUsers, None, cursor, cnx) q = queue.Queue() for u in users: user = u[0].decode("utf-8") #seen.add(user) MakeSeen(user) nonbot.add(user) q.put(user) print("Added to network from DB: {}".format(user)) global testcounter while not q.empty(): # ensure we haven't gone over browse limit primary_count += 1 if primary_count >= BROWSE_LIMIT: try: browser.quit() crawler = Browser('firefox', headless=True) crawler.visit("http://gab.ai") crawler.driver.implicitly_wait(3) username = "******" password = "******" crawler.find_by_name("username").fill(username) crawler.find_by_name("password").fill(password) crawler.find_by_text("Login")[1].click() browser = crawler primary_count = 0 print("PRIMARY BROWSER RESET") except: print("failed to reset primary browser") user = q.get() # known not to be bot testcounter += 1 print("\nSCRAPING " + user) print("grabbing followings") followings = AddNeighbors(user, False) # NOTE: THIS FILTERS BOTS ON ITS OWN print("grabbing followers") followers = AddNeighbors(user, True) print("found {} following".format(len(followings))) print("found {} followers".format(len(followers))) for f_raw in (followings + followers): try: f = f_raw.text[1:] if not UserSeen(f): # if user not yet seen .. IF SLOW, WE CAN MAKE THIS IF NOT USERINDB print("\nappending user {} to the queue...".format(f)) q.put(f) MakeSeen(f) print("there are now {} members in the queue".format(len(q))) except: print("error adding user to queue") continue
def before_all(context): django.setup() context.test_runner = DiscoverRunner() context.test_runner.setup_test_environment() context.browser = Browser('firefox', headless=True)
#!usr/bin/python from splinter.browser import Browser import re import time br = Browser() br.visit("http://www.160by2.com/Index") br.fill('username', 'YOUR_PHONE_NUMBER_USED_TO_CREATE_160BY2_ACCOUNT') br.fill('password', 'YOUR_ACCOUNT_PASSWORD') button = br.find_by_name("") button.click() stringurl = br.url stringurl2 = stringurl.split("id=", 1)[1] id = stringurl2.split("id=", 1)[1] jsstring = "window.parent.openPage('SendSMS?id=" + id + "', 'aSendSMS', 'aSMS', 'ulSMS')" br.execute_script(jsstring) time.sleep(8) br.execute_script(jsstring) print "JS Executed" time.sleep(8) with br.get_iframe('by2Frame') as iframe: iframe.fill( "sendSMSMsg", "Well, I guess robot's do send SMSes when spandan wants them to ;)") iframe.find_by_tag("input")[10].fill( "THE_PHONE_NUMBER_YOU_WANT_TO_SEND_SMS_TO") button2 = iframe.find_by_id("btnsendsms").first print button2.value button2.click()
def setUpClass(cls): cls.browser = Browser('webdriver.firefox')
def before_all(context): context.browser = Browser('chrome', headless=False)
def before_all(context): django.setup() context.test_runner = DiscoverRunner() context.test_runner.setup_test_environment() context.browser = Browser('phantomjs')
def start(url, username, pwd): b = Browser(driver_name="firefox") b.visit(url) login(b, username, pwd) try: b.find_by_id("btn-reservation").click() except AttributeError as e: print('Error:', e) time.sleep(10) while True: loop(b, url) if b.is_element_present_by_id("tryBtn"): b.find_by_id("tryBtn").click() time.sleep(4) elif b.title == u"订单结算页 -京东商城": b.find_by_id("order-submit").click() else: print(u'恭喜你,抢购成功') break
class Buy_Tickets(object): # 初始化 def __init__(self, username, passwd, order, passengers, dtime, starts, ends): self.url_template = ( 'https://kyfw.12306.cn/otn/leftTicket/query{}?leftTicketDTO.' 'train_date={}&' 'leftTicketDTO.from_station={}&' 'leftTicketDTO.to_station={}&' 'purpose_codes=ADULT' ) self.username = username self.passwd = passwd # 日期 self.dtime = dtime # 乘客名 self.passengers = passengers.split(',') # 起始地和终点 self.starts = Station_Parse.parse_station().parse(starts) self.ends = Station_Parse.parse_station().parse(ends) if self.starts is None or self.ends is None: self.title = '提示' self.message = '请输入有效的车站名' messagebox.showinfo(self.title, self.message) # 车次 self.order = order if self.order != '0': self.order = self.order_transfer() while(not self.order): sleep(1) self.order = self.order_transfer() # 起始地和终点转为cookie值 self.starts = self.Get_Cookies()[0] + '%2C' + self.starts self.ends = self.Get_Cookies()[1] + '%2C' + self.ends self.login_url = 'https://kyfw.12306.cn/otn/login/init' self.initMy_url = 'https://kyfw.12306.cn/otn/index/initMy12306' self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init' self.driver_name = 'chrome' self.executable_path = 'D:\\Python35\\Scripts\\chromedriver.exe' # 登录 def login(self): self.driver.visit(self.login_url) self.driver.fill('loginUserDTO.user_name', self.username) self.driver.fill('userDTO.password', self.passwd) self.title = '提示' self.message = '请在自动打开的浏览器中输入验证码!' messagebox.showinfo(self.title, self.message) while True: if self.driver.url != self.initMy_url: sleep(1) else: break # 车次转换 def order_transfer(self): self.headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3294.6 Safari/537.36'} self.res = requests.get(self.url_template.format('A', self.dtime, self.starts, self.ends), headers=self.headers, verify=False) try: try: self.trains = self.res.json()['data']['result'] except: self.res = requests.get(self.url_template.format('Z', self.dtime, self.starts, self.ends), headers=self.headers, verify=False) self.trains = self.res.json()['data']['result'] self.num = 0 for self.train in self.trains: self.data_list = self.train.split('|') if self.data_list[3] == self.order: break self.num += 1 if self.num == len(self.trains): self.title = '提示' self.message = '您输入的车次不存在,系统将自动选择任意车次!' messagebox.showinfo(self.title, self.message) self.num = '0' return self.num except: title = '提示' message = '因为网络原因正在重新尝试!' messagebox.showinfo(title, message) return None # 中文转Unicode def to_unicode(self, string): self.uni = '' for s in string: self.uni += hex(ord(s)).upper().replace('0X', '%u') return self.uni # 将起始地和终点转化为相应的Cookies def Get_Cookies(self): return [self.to_unicode(self.starts), self.to_unicode(self.ends)] # 买票 def start_buy(self): self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) self.driver.driver.set_window_size(700, 500) self.login() self.driver.visit(self.ticket_url) try: self.title = '提示' self.message = '开始购票!' messagebox.showinfo(self.title, self.message) # 加载查询信息 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() if self.order != '0': while self.driver.url == self.ticket_url: self.driver.find_by_text('查询').click() # self.title = '提示' # self.message = '开始尝试预订...' # messagebox.showinfo(self.title, self.message) try: self.driver.find_by_text('预订')[self.order].click() sleep(1.5) except: # self.title = '提示' # self.message = '预订失败...' # messagebox.showinfo(self.title, self.message) continue else: while self.driver.url == self.ticket_url: self.driver.find_by_text('查询').click() # self.title = '提示' # self.message = '开始尝试预订...' # messagebox.showinfo(self.title, self.message) try: for i in self.driver.find_by_text('预订'): i.click() sleep(1) except: # self.title = '提示' # self.message = '预订失败...' # messagebox.showinfo(self.title, self.message) continue self.title = '提示' self.message = '开始选择用户~~~' messagebox.showinfo(self.title, self.message) sleep(1) for p in self.passengers: self.driver.find_by_text(p).last.click() sleep(0.5) if p[-1] == ')': self.driver.find_by_id('dialog_xsertcj_ok').click() self.title = '提示' self.message = '提交订单中...' messagebox.showinfo(self.title, self.message) sleep(1) self.driver.find_by_id('submitOrder_id').click() sleep(2) self.title = '提示' self.message = '确认选座中...' messagebox.showinfo(self.title, self.message) self.driver.find_by_id('qr_submit_id').click() self.title = '提示' self.message = '预订成功...' messagebox.showinfo(self.title, self.message) self.content = '恭喜您抢票成功,请在半小时内完成支付!!!' _ = os.system('mshta vbscript:createobject("sapi.spvoice").speak("%s")(window.close)' % self.content) except: self.title = '提示' self.message = '出现了一点小错误,可能是输入信息有误导致的...' messagebox.showinfo(self.title, self.message)
NOTE: you maybe want to change the webdriver, " Browser() ", and add your prefered driver, Browser('webdriver.chrome') for example, but by default is always set as webdriver.firefox NOTE: choose one url that contains broken links to see the response NOTE: this is a basic code, you can improve it and do what you want, believe, you could do almost everything :) More information, see the docs: http://splinter.cobrateam.info/docs/ """ from splinter.browser import Browser from splinter.request_handler.status_code import HttpResponseError browser = Browser() # Visit URL url = "http://splinter.cobrateam.info/" browser.visit(url) # Get all links in this page urls = [a['href'] for a in browser.find_by_tag('a')] # Visit each one link and verify if is ok for url in urls: try: browser.visit(url) if browser.status_code.is_success(): print '(', browser.status_code.code, ') visit to', url, 'was a success!' except HttpResponseError, e: print '(', e.status_code, ') visit to', url, 'was fail! Error:', e.reason browser.quit()
# f.write(i) # f.write('\n') # return all_url #转发特定微博 def forwardMoney(b): time.sleep(10) b.visit("https://m.weibo.cn/1255795640/4192089996527286") time.sleep(10) oneForward(b) for i in range(3): username, password = login(i) b = Browser(driver_name='chrome') b.visit( "https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F" ) time.sleep(5) try: b.find_link_by_href( "https://passport.weibo.cn/signin/other?r=http%3A%2F%2Fm.weibo.cn%2F" ).click() except: print("找不到第三方账号按钮") time.sleep(2) try: b.find_link_by_href( "https://passport.weibo.com/othersitebind/authorize?entry=mweibo&site=qq&res=other&callback=http%3A%2F%2Fm.weibo.cn%2F"
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())
class BrushTicket(object): """买票类及实现方法""" def __init__(self, passengers_number): """定义实例属性,初始化""" cp = ConfigParser() cp.read("conf/city.conf", encoding='UTF-8') sections = cp.sections() city = sections[0] key_list = cp.options(city) self.city_dict = {} for key in key_list: self.city_dict[key] = cp.get(city, key) cp = ConfigParser() cp.read("conf/12306.conf", encoding='UTF-8') sections = cp.sections() pessenger = sections[passengers_number] # 乘客姓名 self.passengers = cp.get(pessenger, 'name') # 起始站和终点站 self.from_station = self.city_dict[cp.get(pessenger, 'from_station')] self.to_station = self.city_dict[cp.get(pessenger, 'to_station')] # 乘车日期 self.from_time = cp.get(pessenger, 'from_time') # 车次编号 self.number = cp.get(pessenger, 'coach_number') seat_type = cp.get(pessenger, 'seat_type') # 座位类型所在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 = cp.get(pessenger, '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): """登录功能实现,识别验证码 可以使用12306APP 扫一扫登录""" 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) try: print('开始刷票……') title_start = '开始抢票' # self.send_mail(self.receiver_email, '开始抢票' + self.from_time + # '的车票,请关注邮箱抢票通知', title_start) # 加载车票查询信息 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: current_tr = self.driver.find_by_xpath( '//tr[@datatran="' + self.number + '"]/preceding-sibling::tr[1]') if current_tr: if current_tr.find_by_tag('td')[ self.seat_type_index].text == '--': print('无此座位类型出售,已结束当前刷票,请重新开启!') sys.exit(1) elif current_tr.find_by_tag('td')[ self.seat_type_index].text == '无': print('无票,继续尝试……') sleep(1) else: # 有票,尝试预订 print('刷到票了(余票数:' + 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: # 选择用户 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('预订成功,请及时前往支付……') # 发送通知信息 title = '抢票票成功!!' self.send_mail( self.receiver_email, '恭喜您,抢到了' + self.from_time + '的车票,请及时前往12306支付订单!', title) else: print('不存在当前车次【%s】,已结束当前刷票,请重新开启!' % self.number) sys.exit(1) except Exception as error_info: print(error_info) except Exception as error_info: print(error_info) def send_mail(self, receiver_address, content, title): mail_host = 'smtp.163.com' # 163用户名 mail_user = '******' # 密码(部分邮箱为授权码) mail_pass = '******' # 邮件发送方邮箱地址 sender = '*****@*****.**' # 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发 receivers_list = ['*****@*****.**', receiver_address] email = Email(mail_host, mail_user, mail_pass) for receiver in receivers_list: # 设置email信息 # 邮件内容设置 smtpObj = email.getConnection() message = MIMEText(str(content), 'plain', 'utf-8') # 邮件主题 message['Subject'] = title # 发送方信息 message['From'] = sender # 接受方信息 message['To'] = receiver email.send_email(smtpObj, sender, receiver, message)
def function(): b = Browser("chrome") b.visit(AppSite) dictAppSite = { '': '澳大利亚', # 选择访问国 '': '中国', # 选择居住国 '': '澳大利亚签证中心-成都', # 地点 '': 'Work and Holiday Visa', # 选择签证;类别 } b.fill_form(dictAppSite) button = b.find_by_value(u"继续") button.click() b.visit(AppAdd) dictAppAdd = { '': 'G11111111', # 护照号码 '': '1/1/1999', # 出生日期 '': '1/1/2025', # 护照失效期 '': 'CHINA', # 选择国籍 '': '阿', # 名 '': '伟', # 姓 '': '男性', # 性别 '': '+8612345678900', # 电话号码 '': '*****@*****.**' # 电子邮箱 # 验证码 } b.fill_form(dictAppSite) button = b.find_by_value(u"提交") button.click() b.visit(AppData) dictAppData = { '': '7/15/2019', # 日期槽 '': '8:30-8:45' # 时间槽 # 验证码 } b.fill_form(dictAppAdata) button = b.find_by_value(u"提交") button.click()
def __init__(self, passengers_number): """定义实例属性,初始化""" cp = ConfigParser() cp.read("conf/city.conf", encoding='UTF-8') sections = cp.sections() city = sections[0] key_list = cp.options(city) self.city_dict = {} for key in key_list: self.city_dict[key] = cp.get(city, key) cp = ConfigParser() cp.read("conf/12306.conf", encoding='UTF-8') sections = cp.sections() pessenger = sections[passengers_number] # 乘客姓名 self.passengers = cp.get(pessenger, 'name') # 起始站和终点站 self.from_station = self.city_dict[cp.get(pessenger, 'from_station')] self.to_station = self.city_dict[cp.get(pessenger, 'to_station')] # 乘车日期 self.from_time = cp.get(pessenger, 'from_time') # 车次编号 self.number = cp.get(pessenger, 'coach_number') seat_type = cp.get(pessenger, 'seat_type') # 座位类型所在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 = cp.get(pessenger, '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)