1、环境搭建
1)安装Python
链接:https://pan.baidu.com/s/1bVGegGjINkSgzbQ6yx1KrA
提取码:z23o
2)在idea上安装插件
3)安装 selenium
pip install selenium-3.12.0-py2.py3-none-any
4)准备浏览器驱动器
IEDriverServer、chromedriver(google)、geckodriver(Firefox)
均无需安装,复制到Python36-32 中即可
2、编写脚本控制浏览器
1)打开关闭浏览器
2)打开 Url
driver.get("url 地址")
3)等待时间
4)切换框架/窗口
5)定位页面元素
6)页面元素的属性和方法
注册操作
from selenium import webdriver from selenium.webdriver.support.select import Select from time import sleep # 批量插入 #打开浏览器 driver = webdriver.Chrome() # 打开网址 driver.get("http://192.168.109.142:1080/webtours") sleep(3) for i in range(20,30): # 切换框架 driver.switch_to.default_content() driver.switch_to.frame("body") driver.switch_to.frame("info") #定位元素 driver.find_element_by_xpath("//a[@href='login.pl?username=&password=&getInfo=true']").click() sleep(2) #输入信息 driver.find_element_by_name("username").send_keys("ls0",i) driver.find_element_by_name("password").send_keys("123456") driver.find_element_by_name("passwordConfirm").send_keys("123456") driver.find_element_by_name("firstName").send_keys("l") driver.find_element_by_name("lastName").send_keys("s0",i) driver.find_element_by_name("address1").send_keys("london") driver.find_element_by_name("address2").send_keys("usa") #点击register driver.find_element_by_name("register").click() sleep(3) #点击 continue driver.find_element_by_xpath("//img[@src='/WebTours/images/button_next.gif']").click() sleep(2) #切换框架 driver.switch_to.default_content() driver.switch_to.frame("body") driver.switch_to.frame("navbar") #点击 SignOff driver.find_element_by_xpath("//img[@alt='SignOff Button']").click() sleep(2) #关闭浏览器,以及驱动程序 driver.quit()
3ww21 123456 tian bei
3ww12 123456 tian bei
3ww13 123456 tian bei
3ww14 123456 tian bei
参数化
file = open("e:/zfile/user.txt",'r')
for f in file:
split = f.split(" ")
username = split[0]
password = split[1]
first = username[0:1]
last = username[1:]
city = split[2]
country = split[3]
file.close()
页面展示
订票操作自动化
from selenium import webdriver from selenium.webdriver.support.select import Select from time import sleep # 添加了检查点 def testSuccess(drivernow ,loca_1,loca_2,message,expect): drivernow.switch_to.default_content() drivernow.switch_to.frame(loca_1) drivernow.switch_to.frame(loca_2) actually = drivernow.find_element_by_tag_name("body").text if expect in actually: print(message,"检查点-成功") else: print(message,"检查点-失败,"+"expect:"+expect+",actually:"+actually) #打开浏览器 driver = webdriver.Chrome() # 打开网址 driver.get("http://192.168.109.142:1080/webtours") sleep(3) testSuccess(driver,"body","info","是否成功进入页面:","Welcome to the Web Tours site.") # 登录 driver.switch_to.default_content() driver.switch_to.frame("body") driver.switch_to.frame("navbar") driver.find_element_by_name("username").send_keys("zs") driver.find_element_by_name("password").send_keys("123456") driver.find_element_by_name("login").click() sleep(3) #判断登录成功 testSuccess(driver,"body","info","是否登录成功:","Welcome") driver.switch_to.default_content() driver.switch_to.frame("body") driver.switch_to.frame("navbar") # 点击进入购票 driver.find_element_by_xpath("//img[@alt="Search Flights Button"]").click() sleep(3) #判断进入购票 testSuccess(driver,"body","info","是否进入购票界面::","Departure City :") # 填写购票信息 driver.switch_to.default_content() driver.switch_to.frame("body") driver.switch_to.frame("info") # 下拉列表 # 出发地、目的地 Select(driver.find_element_by_name("depart")).select_by_visible_text("Paris") Select(driver.find_element_by_name("arrive")).select_by_visible_text("London") # 出发时间 返回时间 driver.find_element_by_name("departDate").send_keys("01/21/21") driver.find_element_by_name("returnDate").send_keys("01/23/21") # 乘客数量、 driver.find_element_by_name("numPassengers").clear() driver.find_element_by_name("numPassengers").send_keys("1") # 是否訂飯程機票 driver.find_element_by_name("roundtrip").click() # 单选按钮 # //靠边 driver.find_elements_by_name("seatPref")[1].click() # 挑选座位 driver.find_elements_by_name("seatType")[1].click() # 点击确定 driver.find_element_by_name("findFlights").click() sleep(3) #判断订票点击成功 testSuccess(driver,"body","info","判断订单是否提交 :","Flight departing from") # //选取航班 outboundFlight returnFlight # 去时候 driver.find_elements_by_name("outboundFlight")[1].click() # 回来时候 driver.find_elements_by_name("returnFlight")[1].click() # 点击确定 reserveFlights driver.find_element_by_name("reserveFlights").click() sleep(3) # //确认个人信息 # 点击确定buyFlights driver.find_element_by_name("buyFlights").click() sleep(3) # 订票成功,展示订单详情 testSuccess(driver,"body","info","订票成功,展示订单详情:","Thank you for booking through Web Tours."); # //定另外一张 driver.find_element_by_name("Book Another").click() sleep(3) # 关闭浏览器 driver.quit()