selenium自動新增店鋪
阿新 • • 發佈:2018-04-02
form clas option 最大 店鋪 connector frame side 滾動
說明:僅作為參考練習,代碼中涉及數據均為測試數據。
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("新增店鋪失敗")
selenium自動新增店鋪