说明:仅作为参考练习,代码中涉及数据均为测试数据。
1 from selenium import webdriver 2 from selenium.webdriver import ActionChains 3 # from selenium.webdriver.support.ui import WebDriverWait 4 # from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 5 import time 6 import mysql.connector 7 8 #连接数据库 9 conn = mysql.connector.connect(host=‘192.168.1.10‘, port = 3306, user=‘root‘, password=‘abcd@1234‘, database=‘lingyunzhubao‘) 10 # 操作游标, 获取python操作者mysql命令 11 cursor = conn.cursor() 12 # 测试店铺 13 shopname = "测试20170927" 14 query_sql =‘select * from jch_store t where t.store_name= "%s"‘ %shopname #此处没有逗号,加逗号会变成tuble类型 15 delete_sql = ‘delete from jch_store where store_name = "%s"‘ %shopname 16 cursor.execute(query_sql) 17 result =cursor.fetchall() 18 if result is not None: 19 print("删除已有店铺") 20 cursor.execute(delete_sql) 21 conn.commit() 22 else: 23 print(shopname +" 该店铺可新增") 24 25 # 火狐浏览器打开 26 # driver = webdriver.Firefox() 27 # driver = webdriver.Ie() 28 29 # 谷歌,多加几行是为了去掉提示:Chrome正收到自动测试软件的控制 30 options = webdriver.ChromeOptions() 31 options.add_argument(‘disable-infobars‘) 32 driver = webdriver.Chrome(chrome_options=options) 33 34 driver.get("http://192.168.1.10:8080/jcho2o/login/login.do") 35 36 #最大化浏览器 37 driver.maximize_window() 38 #等待浏览器打开 39 print("登录麦珠宝后台") 40 time.sleep(3) 41 driver.find_element_by_id("userName").send_keys("13022025612") 42 driver.find_element_by_id("passWord").send_keys("123456") 43 time.sleep(1) 44 driver.find_element_by_xpath(‘//*[@id="form1"]/div[6]/button‘).click() 45 time.sleep(1) 46 print("点击[店铺信息设置]") 47 driver.find_element_by_xpath(‘//*[@id="side-menu"]/li[1]/a/span‘).click() 48 print("进入[店铺信息界面],等待3秒,点击[新增店铺]") 49 time.sleep(0.5) 50 51 # 切换frame 52 driver.switch_to.frame("iframe0") 53 # print (driver.find_element_by_xpath(‘//div[text()="新增店铺"]‘).text) 54 driver.find_element_by_xpath(‘//div[text()="新增店铺"]‘).click() # 尼玛终于找到了 55 56 print("进入新增页面,等待3秒后,输入数据") 57 # 切回默认页面 58 driver.switch_to.default_content() 59 time.sleep(0.5) 60 driver.switch_to.frame("iframe15") 61 time.sleep(0.5) 62 63 driver.find_element_by_xpath(‘//*[@id="txt_shop_name"]‘).send_keys("测试20170927") 64 time.sleep(0.3) 65 # 点击店铺地址下拉框 66 driver.find_element_by_xpath(‘//span[text()="请选择省市区"]‘).click() 67 #等待3秒,选择省份 68 time.sleep(0.5) 69 driver.find_element_by_xpath(‘//a[text()="北京市"]‘).click() 70 time.sleep(0.5) 71 driver.find_element_by_xpath(‘/html/body/div/div/div[4]/div[1]/div/div/div[2]/div[2]/dl/dd/a[1]‘).click() 72 time.sleep(0.5) 73 driver.find_element_by_xpath(‘/html/body/div/div/div[4]/div[1]/div/div/div[2]/div[3]/dl/dd/a[4]‘).click() 74 time.sleep(0.5) 75 driver.find_element_by_id("txt_shop_address_details").send_keys("具体店铺地址") 76 print("填写店铺收货资料") 77 # 滑动滚动条下滑到底部 78 # scroll_to_position = driver.find_element_by_xpath(‘//button[text()="保存"]‘) 79 # ActionChains(driver).move_to_element(scroll_to_position).perform() 80 # time.sleep(1) 81 82 #将页面滚动条拖到底部 83 js="var q=document.documentElement.scrollTop=10000" 84 driver.execute_script(js) 85 time.sleep(2) 86 87 # 输入店铺收货人 88 driver.find_element_by_id("txt_addressee_name").send_keys("店铺收货人") 89 # 输入店铺联系方式 90 driver.find_element_by_id("txt_addressee_phone").send_keys("13022025810") 91 #输入店铺收货地址 92 driver.find_element_by_xpath("/html/body/div/div/div[10]/div[1]/span/span[1]").click() 93 time.sleep(0.5) 94 driver.find_element_by_xpath("/html/body/div/div/div[10]/div[1]/div/div/div[2]/div[1]/dl[1]/dd/a[6]").click() 95 96 time.sleep(0.5) 97 driver.find_element_by_xpath("/html/body/div/div/div[10]/div[1]/div/div/div[2]/div[2]/dl/dd/a[1]").click() 98 time.sleep(0.5) 99 driver.find_element_by_xpath("/html/body/div/div/div[10]/div[1]/div/div/div[2]/div[3]/dl/dd/a[3]").click() 100 time.sleep(0.5) 101 driver.find_element_by_id("txt_address_detail").send_keys("琶洲国际采购中心 test") 102 time.sleep(0.5) 103 driver.find_element_by_xpath(‘//button[text()="保存"]‘).click() 104 time.sleep(0.5) 105 driver.switch_to.default_content() 106 107 108 cursor.execute(query_sql) 109 # fetchall()则是接收全部的返回结果行 row就是在python中定义的一个变量,用来接收返回结果行的每行数据 110 new_result = cursor.fetchall() 111 if new_result is not None: 112 print("新增店铺成功") 113 else: 114 print("新增店铺失败")