Python+selenium+mysql (dynamic fetch data, database interaction)

Source: Internet
Author: User

First, create a database connection
#!coding:utf-8ImportPymysql" "Python3 no longer support the MYSQLDB way to access the MySQL database, you can use Pymysql way to connect: 1, import Pymysql 2, open the database connection conn = Pymys Ql.connect (host= ' 10.*.*.* ', user= ' root ', password= ' 123456 ', db= ' Self_dev ', charset= ' UTF8 ', cursorclass= Pymysql.cursors.DictCursor) Note: Where cursorclass=pymysql.cursors.dictcursor is optional, the configuration is how each field is displayed,    Display in the form of a dictionary (easy access by column name), in the form of a default tuple. 3. Use the cursor () method to get the operation cursor cur = connection.cursor () 4, SQL query Statement sql = "SELECT * FROM table t where t.name= ' CPPCC ' "5, Execute SQL statement cur.execute (SQL) 6, get all records list rows = Cur.fetchall () 7, output 8, close database connection connection.c Lose ()" "classdbclection:connection= Pymysql.connect (host='10.*.*.*', the user='Root', Password='123456', DB='Self_dev', CharSet='UTF8', Cursorclass=pymysql.cursors.DictCursor)defGetorgid (self): cur=self.connection.cursor () SQL="SELECT t.id from org t where t.name= ' Automation Add (administrative area) ' and t.status= ' 1 '"        Try: Cur.execute (SQL) Results=Cur.fetchall () forRowinchresults:org_id= row['ID']                returnorg_id#print (org_id)        except:            Print('error:unable to fetch data') Self.connection.close ()" "if __name__ = = ' __main__ ': db = dbclection () res = Db.getorgid () print (res)" "
Second, the Getorgid () method under the dbclection created above is quoted
# !coding:utf-8  from Import Webdriver  from Import Sleep Import UnitTest  from Import Actionchains  from Import Select  from Import Dbclection


Omit--------------------------Body

defTest_add_fenwei (self): U" "Login" "username='Org-admin'Password='123456'Self.login (Username,password) sleep (2)        #Enter the organization management interfaceSelf.dr.get (Self.org_url) sleep (2) db = Dbclection () xzjd_org = Db.getorgid ()#print (xzjd_org)        Self.by_xpath ('//*[@id = '%s ']/i '% xzjd_org). Click () Sleep (2) Self.dr.find_element_by_partial_link_text ('Automation Add (GEO)'). Click () Sleep (2) self.by_id ('adddept'). Click () Sleep (3) self.by_id ('Orgname_add'). Send_keys ('Automation Add (category)') Sleep (2) Select (self.by_id ("Govdepttype_add"). Select_by_visible_text ("category") Sleep (2) self.by_id ('Savedeptadd'). Click ()


Note: The variable needs to be formatted in% when it is used in page positioning, otherwise it cannot be passed in.
For example:
 
= Dbclection ()
 xzjd_org = Db.getorgid () # Print (xzjd_org)        self.by_xpath ('//*[@id = '%s']/i '  %xzjd_org). Click () 



Python+selenium+mysql (dynamic fetch data, database interaction)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.