Getting Started
What is Python?
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language.
#!/usr/bin/python3 print("Hello, Python!")
Installation
Downloading and Installing Selenium
>pip install selenium Note:"Script" dir. not's found in Python 3.3 version after installation.Following is solution(Installing easy_install and pip): Step1,To https://pypi.python.org/pypi/setuptools Download ez_setup.py; Step2,>python ez_setup.py Step3,>easy_install pip .Or Directly dowload pip-9.0.1.tar.gz,>python setup.py install --Updating pip >python -m pip install --upgrade pip
Installing pymssql
>pip install pymssql
Connecting SQL Server by pymssql
sqlserver class
sql_server.py
##### #SQL Server class. ##### import pymssql class sqlserver: #Connect. def __init__(self,host,user,password,database): #server url or ip. self.host = host #User name. self.user = user #Password. self.password = password #Database. self.database=database def __get_connect(self): #Connect SQL Server. if not self.database: raise(NameError,"没有连接数据库信息") self.conn = pymssql.connect(self.host, self.user, self.password, self.database,charset="utf8") cursor = self.conn.cursor() if not cursor: raise(NameError,"未能连接到数据库,请检查!") else: return cursor #Close connect. def conn_closed(self): self.conn.close() #Query data. def query(self,sql): cur=self.__get_connect() cur.execute(sql) dataList=cur.fetchall() self.conn_closed() #Closing connect. return dataList
test.py
from selenium import webdriver from selenium.webdriver.common.keys import Keys from splinter.browser import Browser from time import sleep from sql_server import sqlserver #Login def login(driver): driver.get("http://localhost:8080/chicago/") #Enter to user name. userName=driver.find_element_by_name("userName") userName.send_keys("admin") sleep(1) #Enter to pwd. password=driver.find_element_by_name("password") password.send_keys("123456") sleep(1) #Login to site. password.send_keys(Keys.RETURN) #Get out data of vehicle. def get_out_card_data(conn): cardInfo=conn.query("SELECT io.card_no from chicago_io io where card_no like 'dm16%' and io.out_time is NULL order by io.card_no") return cardInfo #Begin out def begin_out(driver,outData): driver.find_element_by_xpath("//div[@id='ext-gen20']/div[@id='002']").click() driver.find_element_by_link_text("车辆进出管理").click() driver.find_element_by_link_text("车辆出场操作").click() sleep(5) card_no=driver.find_element_by_name("truckIo.cardNo") submit_button=driver.find_element_by_id("ext-gen26") for card in outData: card_no.send_keys(card) submit_button.click() sleep(3) #Begin in #def begin_in(inData): #main def main(): driver=webdriver.Chrome() #Login site. login(driver) #Connection. conn=sqlserver("localhost","sa","123456","chicago5") #Get data of vehicle out. outData=get_out_card_data(conn) # cardStr="" # for row in outData: # cardStr+=''.join(row)+"," outList=[]#Out of vehicle list. inList=[]#In of vehicle list. number=1 if outData: for row in outData: card_no=int(row[0][5:7]) outList.append(card_no) while (number<50): if (number!=card_no): # print(number) inList.append(number) number+=1 else: while (number<50): inList.append(number) number+=1 begin_out(driver,outList) print(inList) if __name__ == "__main__": main()
Solving the problem
SyntaxError: Non-ASCII character '\xc2' in file
Solving: add to header of file #coding:utf-8 or #coding=utf-8
Refrences
https://wiki.python.org/moin/SQL%20Server https://www.tutorialspoint.com/python https://www.tutorialspoint.com/python3