一、简单的例子
业务步骤:
1. 访问项目首页 : http://101.34.221.219:8010/
2. 选择登录按钮 : a.text = '登录'
3. 点击登录按钮 : a.click()
4. 定位账号输入框 xpath = '/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[1]/input'
5. 输入账号
6. 定位密码输入框 xpth =
'/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[2]/div/input'
7. 输入密码
8. 定位登录按钮 xpah = '/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[3]/button'
9. 点击登录
1 from selenium.webdriver.common.by import By 2 from webdriver_helper import get_webdriver 3 driver = get_webdriver() # 启动浏览器 4 driver.get("http://101.34.221.219:8010/") # 1 5 el = driver.find_element(By.LINK_TEXT, "登录") # 6 el.click() # 3 7 el = driver.find_element(By.XPATH,"/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[1]/input") # 4 8 el.send_keys("beifan") # 5 9 el = driver.find_element(By.XPATH,"/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[2]/div/input") 10 el.send_keys("123123") # 7 11 el = driver.find_element(By.XPATH,"/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[3]/button") 12 el.click() 13 input() 14 driver.quit() # 关闭浏览器
一、复杂的例子
业务步骤:
1.登录
2.选择商品 : text = "vivo X5MAX L 移动4G 八核超薄大屏5.5吋双卡手机vivoX5max"
3.点击商品
4.选择立即购买 xpath = '/html/body/div[4]/div[2]/div[2]/div/div[3]/div[2]/button[1]'
5.处理弹窗
6.选择付款方式 xpath = '/html/body/div[4]/div/div[4]/ul/li[1]/span'
7.处理弹窗
8.选择提交订单按钮 xpath = '/html/body/div[4]/div/div[6]/div/form/div/button'
9.点击提交按钮
10.获取系统提示
11.断言 系统提示的内容是 ”操作成功“
1 from selenium.webdriver.common.by import By 2 from webdriver_helper import get_webdriver 3 def get_msg(): 4 time.sleep(0.5) # 等待-0.5秒 5 el = driver.find_element(By.XPATH, "//p[@class='prompt-msg']") 6 return el.text 7 def alert(): 8 time.sleep(2) 9 driver.switch_to.alert.accept() # 弹窗点击确定
10 driver = get_webdriver() # 启动浏览器 11 driver.maximize_window() 12 driver.get("http://101.34.221.219:8010/") # 1 13 el = driver.find_element(By.LINK_TEXT, "登录") # 14 el.click() # 3 15 el = driver.find_element(By.XPATH,"/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[1]/input") # 4 16 el.send_keys("beifan") # 5 17 el = driver.find_element(By.XPATH,"/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[2]/div/input") 18 el.send_keys("123123") # 7 19 el = driver.find_element(By.XPATH,"/html/body/div[4]/div/div[2]/div[2]/div/div/div[1]/form/div[3]/button") 20 el.click() 21 # 登录成功 22 msg = get_msg() 23 assert msg == "登录成功" 24 time.sleep(0.5) 25 driver.get("http://101.34.221.219:8010/") # 回到首页 26 el = driver.find_element(By.PARTIAL_LINK_TEXT, "vivo") # 选择商品 27 el.click() 28 # 窗口最新打开的切换 29 driver.switch_to.window(driver.window_handles[-1]) 30 time.sleep(0.5) 31 el = driver.find_element(By.XPATH, "/html/body/div[4]/div[2]/div[2]/div/div[3]/div[2]/button[1]") 32 el.click() # 立即购买 33 alert() # 处理弹出 34 el = driver.find_element(By.XPATH,"/html/body/div[4]/div/div[4]/ul/li[1]/span") 35 el.click() # 付款方式 36 alert() # 处理弹窗 37 el = driver.find_element(By.XPATH,"/html/body/div[4]/div/div[6]/div/form/div/button") 38 el.click() # 提交等待 39 msg = get_msg() # 获取系统提示 40 assert msg == "操作成功" 41 input() 42 driver.quit() # 关闭浏览器
标签:body,el,Selenium,driver,html,实例,自动化,div,find From: https://www.cnblogs.com/lzcnblogs/p/16624575.html