selenium案例(1)
下面是源代码:
1 #!/usr/bin/python 2 # -*- coding: utf-8 -*- 3 from selenium import webdriver#第三方webdriver库 4 import time,os #代码休眠时间 5 from selenium.webdriver.common.action_chains import ActionChains #鼠标事件 6 import pickle#cookie处理模块 7 from selenium.webdriver.support.ui import WebDriverWait #显式等待时间 8 from selenium.webdriver.common.by import By 9 from selenium.webdriver.common.keys import Keys 10 from selenium.webdriver.support.ui import Select 11 from selenium.common.exceptions import NoSuchElementException 12 import unittest, re 13 import logging 14 import math #数据运算 15 from selenium.webdriver.support import expected_conditions as EC 16 17 18 driver = webdriver.Chrome()#生成浏览器对象 19 print("打开浏览器成功") 20 driver.implicitly_wait(3)#隐性等待3秒 21 #driver.maximize_window()#浏览器窗口最大化 22 url = "http://www.meizhuyun.com/Home/BookPage/index.html" 23 24 File_object=open("D:\www_meizhu\dl.txt","r",)#实例化File_object文件对象,并且打开 25 lines = File_object.readlines()#返回File_object文件中所有数据的行数 26 27 def Openurl(): 28 time.sleep(2) 29 driver.get(url)#(打开url) 30 print("成功其获取url") 31 #driver.save_screenshot("C:\meizhu2.png")#整个窗口截图保存 32 return "dakaichenggong" 33 34 def Login(): 35 uname=driver.find_element_by_id("requestUsername") 36 psw=driver.find_element_by_id("requestPassword") 37 submit=driver.find_element_by_id("requestSubmit") 38 39 for line in lines: 40 try: 41 username=line.split(",")[0] #遍历文件中每行数据,以列表的形式返回,并在逗号所在处进行分割,最后通过索引返回相关字符串 42 password=line.split(",")[1] #同上 43 except Exception as e: 44 print('error:',e) 45 finally: 46 print(uname.send_keys(username),"校验成功") 47 time.sleep(2) 48 print(psw.send_keys(password),"校验成功") 49 if username == "17094101202": 50 break 51 submit.click() 52 time.sleep(1) 53 uname.clear() 54 psw.clear() 55 56 57 def Add(): 58 time.sleep(3) 59 room = driver.find_element_by_xpath("//*[@id='orderListBody']/tr[1]/td[4]")#401G房jian 60 room.click() 61 def Recorded_single(): 62 time.sleep(3) 63 64 name =driver.find_element_by_xpath("//*[@id='addOrderRoom']/table/tbody/tr[2]/td[1]/input")#入住人 65 phone =driver.find_element_by_xpath("//*[@id='addOrderRoom']/table/tbody/tr[2]/td[2]/div/input")#电话号码 66 idcard = driver.find_element_by_xpath("//*[@id='addOrderRoom']/table/tbody/tr[2]/td[3]/input")#身份证号 67 Amount_money =driver.find_element_by_xpath("//*[@id='addOrderReceive']/tbody/tr/td[3]/div/input")#房间金额数 68 sumbit_from = driver.find_element_by_id("submitBook") 69 try: 70 name.send_keys("hh") 71 time.sleep(1) 72 phone.send_keys("13580251014") 73 time.sleep(1) 74 idcard.send_keys("441625199303125438") 75 time.sleep(1) 76 Amount_money.send_keys(100) 77 time.sleep(1) 78 sumbit_from.click() 79 time.sleep(2) 80 except Exception as e: 81 print('error:', e) 82 83 def Cancellation_of_order(): 84 try: 85 room1=driver.find_element_by_xpath("//*[@id='orderListBody']/tr[1]/td[4]") 86 room1.click() 87 time.sleep(3) 88 driver.find_element_by_xpath("//*[@id='initCancel']").click() 89 time.sleep(3) 90 driver.find_element_by_id("cancelOrderConfirm") .click() 91 except Exception as e: 92 print('error:', e) 93 94 95 if "__name__"=="__main__": 96 Openurl() 97 Login() 98 Add() 99 Recorded_single() 100 Cancellation_of_order() 101 driver.quit()