Selenium综合应用

2024-07-03  
0条评论   94浏览

项目介绍

该项目主要通过selenium+python为对电子商务网站进行自动化测试 项目地址及相关资料:https://gitee.com/leeyiru/selenium

内容介绍

网站环境安装 目录为安装测试网站及selenium工具

测试用例设计 分析及编写测试用例

Selenium使用 结合分析网站对selenium的介绍及使用

综合实验

实验要求

设计一套页面操作流程,进行一系列操作,至少包含如下内容:浏览器前进后退,窗口大小操作,获取文本框里的数据或获取静态文本数据并进行处理,获取按钮或复选框的状态,选中某一按钮或复选框,模拟鼠标的单击、双击、右击等操作,模拟键盘的复制、粘贴、输入等操作,对弹出消息框进行确定、取消、截屏操作,切换浏览器窗口和Frame,页面元素截屏操作,验证码获取操作等。

代码

# 作者:
# 华南师范大学21软工8班 李艺儒
# 完成时间:
# 2024/06/13
# 测试环境:
# python3.12,
# selenium4.21.0,
# ddddocr1.5.2

from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from PIL import ImageGrab
import ddddocr

driver = webdriver.Firefox()
driver.get("http://localhost/upload/index.php")

#  窗口大小操作
driver.set_window_size(200, 200)
driver.set_window_position(10, 10)
sleep(3)
# 最小
driver.minimize_window()
sleep(3)
# 最大
driver.maximize_window()
sleep(3)

#  浏览器前进后退操作
# 登录
driver.find_element(By.CSS_SELECTOR,"#ECS_MEMBERZONE > a:nth-child(2) > img:nth-child(1)").click()
sleep(3)
# 后退
driver.back()
url = driver.current_url
sleep(3)
# 前进
driver.forward()
sleep(3)
driver.get(url)

#  对弹出消息框进行确定、取消
driver.find_element(By.NAME,'imageField').click()
sleep(3)
# 确定
driver.switch_to.alert.accept()
driver.find_element(By.ID,'keyword').send_keys('806')
driver.find_element(By.NAME,'imageField').click()
sleep(3)
driver.find_element(By.LINK_TEXT,'P806').click()
sleep(3)
driver.find_element(By.XPATH,'/html/body/div[7]/div[2]/div[1]/div[2]/form/ul/li[9]/a[1]/img').click()
sleep(6)
driver.find_element(By.LINK_TEXT,'删除').click()
sleep(3)
# 取消
driver.switch_to.alert.dismiss()
driver.find_element(By.LINK_TEXT,'首页').click()
sleep(3)

# 获取文本框的数据
driver.find_element(By.LINK_TEXT,"P806").click()
num = driver.find_element(By.ID,"number")
tex1=num.get_attribute("value")
print(f"手机数量是:{tex1}")
sleep(3)

#  获取按钮状态、选中按钮
butt = driver.find_element(By.ID,"spec_value_168")
if not butt.is_selected():
    butt.click()
    sleep(3)
print("总价为", driver.find_element(By.ID,"ECS_GOODS_AMOUNT").text)
sleep(3)

#  模拟鼠标的单击、双击、右击等操作,
# 单击左键留言板
ActionChains(driver).move_to_element(driver.find_element(By.CSS_SELECTOR,"#mainNav > a:nth-child(10)")).click().perform()
sleep(3)
# 单击右键
ActionChains(driver).move_to_element(driver.find_element(By.ID,"keyword")).context_click().perform()
# 单击左键
ActionChains(driver).move_to_element(driver.find_element(By.ID,"keyword")).click().perform()
sleep(3)
# 双击左键
ActionChains(driver).move_to_element(driver.find_element(By.NAME,"user_email")).double_click().perform()
sleep(3)
ema = driver.find_element(By.NAME,"user_email")
ema.send_keys("lee@ecshop.com")
sleep(3)

#  模拟键盘的复制、粘贴、输入等操作,
ema.send_keys(Keys.HOME)
ema.send_keys(Keys.SHIFT, Keys.ARROW_RIGHT)
ema.send_keys(Keys.SHIFT, Keys.ARROW_RIGHT)
ema.send_keys(Keys.SHIFT, Keys.ARROW_RIGHT)
# 复制
ema.send_keys(Keys.CONTROL, 'c')
# 输入
ActionChains(driver).send_keys(Keys.TAB).send_keys(Keys.ARROW_RIGHT).send_keys(Keys.ARROW_RIGHT).send_keys(Keys.TAB).send_keys("询问").perform()
sleep(3)
com = driver.find_element(By.NAME,"msg_content")
com.send_keys("我是")
# 粘贴
com.send_keys(Keys.CONTROL, 'v')
sleep(3)
com.send_keys(",有真人吗")
sleep(3)
driver.get("http://localhost/upload/admin/index.php")
ActionChains(driver).move_to_element(driver.find_element(By.NAME,"username")).click().send_keys("admin").send_keys(Keys.TAB).send_keys("admin123").perform()
sleep(3)

# 验证码获取操作
img = driver.find_element(By.XPATH,'/html/body/form/table/tbody/tr/td[2]/table/tbody/tr[4]/td/img')
img.screenshot("captcha.png")
img_bytes = open("captcha.png", "rb").read()
ocr = ddddocr.DdddOcr()
check_code = ocr.classification(img_bytes)
driver.find_element(By.NAME,'captcha').send_keys(check_code)
driver.find_element(By.CLASS_NAME,"button").click()
sleep(3)

# 下拉列表操作
driver.switch_to.frame('header-frame')
driver.find_element(By.LINK_TEXT,'个人设置').click()
sleep(5)
driver.switch_to.default_content()
driver.switch_to.frame('main-frame')
s1=driver.find_element(By.ID,'all_menu_list')
s2=Select(s1)
for i in range(0,6):
    s2.select_by_index(i)
sleep(5)
s2.deselect_by_visible_text('商品管理')
s2.deselect_by_index(1)
s2.deselect_by_value('comment_manage.php?act=list')
sleep(5)
s2.deselect_all()
s2.select_by_visible_text('    商品类型')
sleep(3)
a=driver.find_element(By.ID,'btnAdd')
if a.is_enabled():
    a.click()
    sleep(5)
s3=driver.find_element(By.ID,'menus_navlist')
s4=Select(s3)
l1=len(s4.options)
for i in range(l1-2,l1):
    s4.select_by_index(i)
    sleep(3)
t1=s4.all_selected_options
for i in range(0,2):
   print(t1[i].text)
print(s4.first_selected_option.text)

#  页面元素截屏操作以及切换frame和窗口
driver.switch_to.default_content()
driver.switch_to.frame('header-frame')
driver.find_element(By.LINK_TEXT,'起始页').click()
sleep(3)
driver.save_screenshot(r".\lee\1.png")
driver.switch_to.default_content()
driver.switch_to.frame("menu-frame")
driver.find_element(By.CSS_SELECTOR,"body").screenshot(r".\lee\2.png")
driver.find_element(By.CSS_SELECTOR,"li.explode:nth-child(1) > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1)").click()
sleep(3)
# 切换frame
driver.switch_to.default_content()
driver.switch_to.frame("main-frame")
driver.find_element(By.XPATH,'//span[text()="夏新N7"]/../../td[11]/a/img').click()
sleep(3)
# 切换窗口
lst=driver.window_handles
driver.switch_to.window(lst[-1])
driver.find_element(By.CSS_SELECTOR,"#ECS_MEMBERZONE > a:nth-child(2) > img:nth-child(1)").click()
sleep(3)
driver.find_element(By.CSS_SELECTOR,".us_Submit").click()
sleep(2)
# 对消息框截屏
ImageGrab.grab().save(r".\lee\3.png")
driver.switch_to.alert.accept()
driver.find_element(By.NAME,"username").send_keys("vip")
driver.find_element(By.NAME,'password').send_keys("vip")
driver.find_element(By.CSS_SELECTOR,".us_Submit").click()
sleep(3)

单元实验

实验1

  1. 从selenium中导入webdriver模块
  2. 从selenium.webdriver.common.by导入By
  3. 从time导入sleep
  4. 启动火狐浏览器
  5. 隐式等待3秒
  6. 打开前台首页http://localhost/upload/index.php
  7. 在首页的文本框中输入“30”
  8. 点击搜索按钮
  9. 等待5秒
  10. 关闭浏览器
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

# 启动火狐浏览器
driver = webdriver.Firefox()

# 隐式等待3秒
driver.implicitly_wait(3)

# 打开前台首页
driver.get("http://localhost/upload/index.php")

# 在首页的文本框中输入“30”
input_element = driver.find_element(By.NAME, "keywords")
input_element.send_keys("30")

# 点击搜索按钮
driver.find_element(By.NAME,"imageField").click()

# 等待5秒
sleep(5)

# 关闭浏览器
driver.quit()

实验2

第一个实验,请用代码写出以下步骤:

(1)启动Firefox

(2)打开前台登录页

(3)输入用户名:vip

(4)输入密码:vip

(5)点击“立即登陆”

(6)等待1秒

(7)点击界面上方“用户中心”

(8)等待2秒

(9)点击左侧“用户信息”

(10)等待5秒

(11)关闭浏览器

第二个实验,请用代码写出以下要求:

(1)启动Firefox

(2)打开注册页

(3)注册成为一个新会员

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import time
from selenium.webdriver.support.select import Select

# 启动Firefox浏览器
driver = webdriver.Firefox()

# 打开前台登录页
driver.get("http://localhost/upload/user.php")
# 等待5秒
time.sleep(1)
# 定位注册按钮并点击
driver.find_element(By.XPATH,"//a[@href='user.php?act=register']").click()
#注册信息
username = driver.find_element(By.NAME,"username")
email = driver.find_element(By.NAME,"email")
password= driver.find_element(By.NAME, "password")
confirm_password= driver.find_element(By.NAME, "confirm_password")
extend_field1= driver.find_element(By.NAME, "extend_field1")
extend_field2= driver.find_element(By.NAME, "extend_field2")
extend_field3= driver.find_element(By.NAME, "extend_field3")
extend_field4= driver.find_element(By.NAME, "extend_field4")
extend_field5= driver.find_element(By.NAME, "extend_field5")
sel_question= driver.find_element(By.NAME, "sel_question")
passwd_answer=driver.find_element(By.NAME, "passwd_answer")
username.send_keys("aaa")
email.send_keys("123@qq.com")
password.send_keys("123456")
confirm_password.send_keys("123456")
extend_field1.send_keys("123456@msn.com")
extend_field2.send_keys("123456")
extend_field3.send_keys("123456")
extend_field4.send_keys("123456")
extend_field5.send_keys("123456")
# 使用 Select 类来操作下拉菜单
select = Select(sel_question)
# 选择对应的选项(例如选择第二个选项)
select.select_by_index(1)  # 索引从0开始,这里选择第二个选项
passwd_answer.send_keys("123456")
button =driver.find_element(By.NAME,"Submit")
button.click();
time.sleep(5)
# 关闭浏览器
driver.quit();

实验3

  1. 启动Firefox浏览器
  2. 打开前台首页
  3. 点击登录按钮,等待3秒
  4. 点击左上角ECSHOP商标,等待3秒
  5. 点击精品推荐区里的第一个商品名称,等待3秒
  6. 点击“高级搜索”,等待3秒
  7. 输入高级搜索页面里的关键字100,等待3秒
  8. 点击“立即搜索”按钮,等待3秒
  9. 点击“夏新N7手机图标”,等待3秒
  10. 点击“查看购物车”,等待3秒
  11. 关闭浏览器
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
driver=webdriver.Firefox()
driver.get('http://localhost/upload/index.php')
driver.find_element(By.XPATH,'/html/body/div[1]/div[2]/ul/li[1]/font/a[1]/img').click()
sleep(3)
driver.find_element(By.XPATH,'/html/body/div[1]/div[1]/a/img').click()
sleep(3)
driver.find_element(By.LINK_TEXT,'三星SGH-F...').click()
sleep(3)
driver.find_element(By.LINK_TEXT,'高级搜索').click()
sleep(3)
driver.find_element(By.ID,'keywords').send_keys(100)
sleep(3)
driver.find_element(By.NAME,'Submit').click()
sleep(3)
driver.find_element(By.XPATH,'/html/body/div[7]/div[2]/div[3]/div/form/div/div/div[5]/a[1]/img').click()
sleep(3)
driver.find_element(By.LINK_TEXT,'查看购物车').click()
sleep(3)
driver.quit()

实验4

要求以下查找元素必须用CSS_SELECTOR选择器进行定位,用代码完成以下步骤:

  1. 启动火狐浏览器
  2. 打开前台首页
  3. 点击“留言板”按钮,等待3秒
  4. 在电子邮件地址文本框里输入vip@163.com
  5. 判断“询问“按钮是否选中,若否,选中”询问“按钮,等待2秒
  6. 在主题文本框中输入“维修“
  7. 在留言内容文本域输入“手机坏了怎么处理?”
  8. 点击我要留言按钮,等待1秒
  9. 关闭浏览器
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
driver=webdriver.Firefox()
driver.get('http://localhost/upload/index.php')
driver.find_element(By.LINK_TEXT,'留言板').click()
sleep(3)
driver.find_element(By.CSS_SELECTOR,'[name="user_email"]').send_keys('vip@163.com')
lst=driver.find_elements(By.CSS_SELECTOR,'[name="msg_type"]')
if not lst[2].is_selected():
    lst[2].click()
sleep(2)
driver.find_element(By.CSS_SELECTOR, '[name="msg_title"]').send_keys('维修')
driver.find_element(By.CSS_SELECTOR, 'textarea').send_keys('手机坏了怎么处理?')
driver.find_element(By.CSS_SELECTOR, '.bnt_blue_1').click()
sleep(1)
driver.quit()

实验5

请用代码实现下列步骤:

  1. 打开留言板页,等待3秒
  2. 把浏览器窗口大小设置为宽度800px和高度600px,等待3秒
  3. 打印窗口左上角位置坐标
  4. 把浏览器窗口最小化,等待3秒,打印窗口大小尺寸
  5. 自定义浏览器窗口位置,把窗口左上角坐标设置为(60px,60px),等待3秒,打印自定义后窗口左上角位置坐标
  6. 把浏览器窗口最大化,等待3秒,打印窗口大小尺寸
  7. 点击“高级搜索“,等待3秒
  8. 后退,等待3秒
  9. 获取当前网页的标题和URL,并打印
  10. 前进,等待3秒
  11. 在地址栏输入获取的URL网址进行访问,等待3秒
  12. 后退,等待3秒
  13. 关闭浏览器
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
driver=webdriver.Firefox()
driver.implicitly_wait(3)
driver.get('http://localhost/upload/message.php')
sleep(3)
driver.set_window_size(800,600)
sleep(3)
print(driver.get_window_position())
sleep(3)
driver.minimize_window()
sleep(3)
print(driver.get_window_size())
driver.set_window_position(60,60)
sleep(3)
print(driver.get_window_position())
driver.maximize_window()
sleep(3)
driver.find_element(By.CSS_SELECTOR,'#searchForm > a:nth-child(4)').click()
sleep(3)
driver.back()
sleep(1)
print(driver.title)
url=driver.current_url
print(url)
driver.forward()
sleep(3)
driver.get(url)
sleep(3)
driver.back()
sleep(3)
driver.quit()

实验6

  1. 打开首页
  2. 输入关键字100,判断“搜索“按钮如果可用,点击搜索按钮,等待3秒
  3. 点击搜索结果区域里的“金立 A30“的商品名称,等待3秒
  4. 打印默认“购买数量“文本框的当前默认值
  5. 获得“商品库存“的台数
  6. 如果台数大于3台,清空“购买数量“文本框,输入3
  7. 判断“数据线“复选框,如果没有被选中,就点击选中它
  8. 判断“线控耳机“复选框,如果没有被选中,就点击选中它
  9. 获得此时的“商品总价“,如果是”¥6210元“,打印”总价计算正确“,否则打印”总价计算错误“。
from selenium import webdriver
from time import sleep
import re
driver = webdriver.Firefox()
driver.get('http://localhost/upload/index.php')
driver.find_element_by_id('keyword').send_keys('100')
a = driver.find_element_by_css_selector('.go')
if a.is_enabled():
    a.click()
    sleep(3)
driver.find_element_by_link_text('金立 A30').click()
sleep(3)
b = driver.find_element_by_id('number')
print("“购买数量“文本框的当前默认值为:", b.get_attribute('value'))
c = driver.find_element_by_xpath('/html/body/div[7]/div[2]/div[1]/div[2]/form/ul/li[1]/dd[2]')
num = int(re.sub(r"\D", "", c.text))
print("商品库存数量为:", num)
if num >= 3:
    b.clear()
    b.send_keys('3')
d = driver.find_element_by_id('spec_value_190')
if not d.is_selected():
    d.click()
e = driver.find_element_by_id('spec_value_189')
if not e.is_selected():
    e.click()
sleep(5)
if driver.find_element_by_id('ECS_GOODS_AMOUNT').text == '¥6210元':
    print("总价计算正确")
else:
    print("总价计算错误")

实验7

以下代码请模拟键盘操作

  1. 打开火狐浏览器
  2. 登录前台首页
  3. 点击“留言板”
  4. 定位电子邮件地址文本框
  5. 输入电子邮件地址:vip@ecshop.com
  6. 按下Home键光标回到行首
  7. Shift+右箭头(→)连续点击三次,选中三个字符vip
  8. Ctrl+c复制,到主题文本框里Ctrl+v粘贴
  9. 到留言内容里输入“我是”、Ctrl+v粘贴,Enter回车换行
  10. 再输入“请问有优惠码?”
  11. 在主题文本框里按下回车
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.common.keys import Keys
driver=webdriver.Firefox()
driver.get('http://localhost/upload/index.php')
driver.find_element(By.LINK_TEXT,'留言板').click()
em=driver.find_element(By.NAME,'user_email')
em.send_keys('vip@ecshop.com')
em.send_keys(Keys.HOME)
em.send_keys(Keys.SHIFT,Keys.ARROW_RIGHT)
em.send_keys(Keys.SHIFT,Keys.ARROW_RIGHT)
em.send_keys(Keys.SHIFT,Keys.ARROW_RIGHT)
em.send_keys(Keys.CONTROL,'c')
ti=driver.find_element(By.NAME,'msg_title')
ti.send_keys(Keys.CONTROL,'v')
co=driver.find_element(By.NAME,'msg_content')
co.send_keys('我是')
co.send_keys(Keys.CONTROL,'v')
co.send_keys(Keys.ENTER)
co.send_keys('请问有优惠码?')
ti.send_keys(Keys.ENTER)

实验8

以下步骤请模拟鼠标和键盘操作

  1. 打开ECSHOP前台首页
  2. 定位到登录按钮,然后单击
  3. 鼠标移动到用户名文本框元素,然后单击
  4. 在文本框里输入“vip”
  5. 按下键盘的Tab键
  6. 等待2秒
  7. 在密码文本框里输入“vip”
  8. 按下键盘的Tab键
  9. 等待2秒
  10. 此时当前焦点位于“立即登陆”这个元素,在这个焦点位置按下回车
  11. 等待5秒
  12. 关闭浏览器
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# 启动浏览器
driver = webdriver.Firefox()

try:
    # 打开网页
    driver.get("http://localhost/upload/index.php")

    # 定位到登录按钮并点击
    login_button = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, "#ECS_MEMBERZONE > a:nth-child(2) > img:nth-child(1)")))
    ActionChains(driver).move_to_element(login_button).click().perform()

    # 输入用户名和密码
    username_field = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.NAME, 'username')))
    password_field = driver.find_element(By.NAME, 'password')

    ActionChains(driver).move_to_element(username_field).click().send_keys("vip").send_keys(Keys.TAB).send_keys(
        'vip').send_keys(Keys.TAB).perform()

    # 提交登录
    submit_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, 'submit')))
    ActionChains(driver).move_to_element(submit_button).click().perform()

    # 等待一段时间,或者使用更具体的等待条件
    time.sleep(5)

finally:
    # 关闭浏览器
    driver.quit()

实验9

  1. 打开ECSHOP前台首页
  2. 点击“搜索”,等待3秒
  3. 切换到消息框
  4. 获得其文本,打印出来
  5. 点击“确定”按钮
  6. 在关键字文本框输入。806
  7. 点击“搜索”,等待3秒
  8. 点击P806商品名称,等待3秒
  9. 点击“加入购物车”,等待6秒
  10. 点击“删除”,等待3秒
  11. 切换到消息框
  12. 获得其文本,打印出来
  13. 点击“取消”按钮
  14. 关闭浏览器
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver=webdriver.Firefox()#启动驱动程序(驱动程序运行起来后,就会启动默认安装目录里的firefox)
driver.get('http://localhost/upload/index.php')
driver.refresh()
a = driver.find_element(By.XPATH,'/html/body/div[4]/form/input[2]')
a.click()
sleep(3)
print(driver.switch_to.alert.text)
driver.switch_to.alert.accept()
driver.find_element(By.XPATH,'//*[@id="keyword"]').send_keys("806")
sleep(1)
a.click()
sleep(3)
driver.find_element(By.XPATH,'/html/body/div[7]/div[2]/div[1]/div/form/div/div/div/a[1]').click()
sleep(3)
driver.find_element(By.XPATH,'/html/body/div[7]/div[2]/div[1]/div[2]/form/ul/li[9]/a[1]').click()
sleep(6)
driver.find_element(By.XPATH,'/html/body/div[7]/div[1]/form/table[1]/tbody/tr[2]/td[7]/a').click()
sleep(3)
print(driver.switch_to.alert.text)
driver.switch_to.alert.dismiss()
sleep(1)
driver.quit()

实验10

  1. 打开ECSHOP前台首页
  2. 点击注册按钮,等待5秒
  3. 点击“用户协议”,等待8秒
  4. 获得当前窗口句柄,保存在变量里
  5. 切换到最新窗口
  6. 在新窗口里点击配送与支付按钮,等待5秒
  7. 点击“EC论坛”,等待5秒,切换到新窗口
  8. 点击“商业授权”,等待5秒,切换到新窗口
  9. 关闭“EC论坛”窗口
  10. 用前面变量里保存的句柄切换回原窗口
  11. 输入用户名“Jack”,等待3秒
  12. 关闭浏览器
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

driver=webdriver.Firefox()
driver.get('http://localhost/upload/index.php')
driver.find_element(By.XPATH,'/html/body/div[1]/div[2]/ul/li[1]/font/a[2]/img').click()
sleep(5)
driver.find_element(By.LINK_TEXT,'用户协议').click()
sleep(8)
a=driver.current_window_handle
list1=driver.window_handles
driver.switch_to.window(list1[-1])
driver.find_element(By.LINK_TEXT,'配送与支付').click()
sleep(5)
driver.find_element(By.LINK_TEXT,'EC论坛').click()
sleep(3)
list2=driver.window_handles
driver.switch_to.window(list2[-1])
driver.find_element(By.LINK_TEXT,'商业授权').click()
sleep(5)
list3=driver.window_handles
driver.switch_to.window(list3[-1])
driver.switch_to.window(list2[-1])
driver.close()
driver.switch_to.window(a)
driver.find_element(By.ID,'username').send_keys('Jack')
sleep(3)
driver.quit()

实验11

  1. 打开后台页http://localhost/upload/admin/index.php
  2. 输入用户名:admin
  3. 输入密码:admin123
  4. 输入万能验证码:0
  5. 点击“进入管理中心”,等待8秒
  6. 点击左侧菜单里的“商品列表”,等待5秒
  7. 点击商品名称为“诺基亚N85”一行后面的“查看”操作按钮,等待5秒
  8. 在新的窗口内,判断“蓝牙耳机”前的复选框是否选中,若否,选中复选框
  9. 点击“加入收藏夹”按钮,等待5秒,若有弹窗,点击“确定”
  10. 关闭“诺基亚N85”窗口
  11. 点击左侧菜单里的“商品回收站”,等待5秒
  12. 点击上方“开店向导”按钮,等待5秒
  13. 点击“退出”,等待5秒
  14. 输入管理员姓名:Jack
  15. 关闭浏览器
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://localhost/upload/admin/index.php")
ActionChains(driver).move_to_element(driver.find_element_by_name("username")).click().send_keys("admin").send_keys(Keys.TAB).send_keys("admin123").send_keys(Keys.TAB).send_keys("0").perform()
driver.find_element_by_class_name("button").click()
sleep(8)
a = driver.current_window_handle
driver.switch_to.frame(driver.find_element_by_id("menu-frame"))
driver.find_element_by_css_selector("li.explode:nth-child(1) > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1)").click()
sleep(5)
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element_by_id("main-frame"))
driver.find_element_by_xpath("/html/body/form/div[1]/table[1]/tbody/tr[3]/td[11]/a[1]/img").click()
sleep(5)
driver.switch_to.window(driver.window_handles[-1])
bt = driver.find_element_by_id("spec_value_158")
if not bt.is_selected():
    bt.click()
driver.find_element_by_css_selector("li.padd:nth-child(9) > a:nth-child(2) > img:nth-child(1)").click()
sleep(5)
driver.switch_to.alert.accept()
sleep(3)
driver.close()
driver.switch_to.window(a)
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element_by_id("menu-frame"))
driver.find_element_by_xpath("/html/body/div[2]/div[1]/ul/li[1]/ul/li[7]/a").click()
sleep(5)
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element_by_id("header-frame"))
driver.find_element_by_xpath("/html/body/div[1]/div[3]/ul/li[9]/a").click()
sleep(5)
driver.find_element_by_css_selector("a.fix-submenu:nth-child(2)").click()
sleep(5)
driver.switch_to.default_content()
driver.find_element_by_name("username").send_keys("Jack")
sleep(3)
driver.quit()

实验12

  1. 打开ECShop后台页
  2. 输入用户名:admin、输入密码:admin123、输入万能验证码:0
  3. 点击“进入管理中心“,等待8秒
  4. 点击界面上方“个人设置“,等待5秒
  5. 选中“设置个人导航“右侧下拉列表的从第1个到第6个选项,等待5秒
  6. 取消文本是“商品管理“的选项,取消第2个选项,取消value是”comment_manage.php?act=list”的选项,等待5秒
  7. 取消右侧下拉列表所有选项
  8. 选择右侧文本是“商品类型“的选项,等待3秒
  9. 如果“增加“按钮变为可用,点击它,等待5秒
  10. 选中“设置个人导航“左侧下拉列表的最后一个选项和倒数第二个选项,等待3秒
  11. 打印左侧下拉列表中所有已被选中的选项的文本
  12. 打印左侧下拉列表中已被选中的选项中第一个的文本
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select

driver = webdriver.Firefox()
driver.get("http://localhost/upload/admin/index.php")
ActionChains(driver).move_to_element(driver.find_element_by_name("username")).click().send_keys("admin").send_keys(Keys.TAB).send_keys("admin123").send_keys(Keys.TAB).send_keys("0").perform()
driver.find_element_by_class_name("button").click()
sleep(8)
driver.switch_to.frame(driver.find_element_by_id("header-frame"))
driver.find_element_by_css_selector("#submenu-div > ul:nth-child(1) > li:nth-child(6) > a:nth-child(1)").click()
sleep(5)
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element_by_id("main-frame"))
list1 = driver.find_element_by_id("all_menu_list")
select1 = Select(list1)
for i in range(0,6):
    select1.select_by_index(i)
sleep(5)
select1.deselect_by_visible_text("商品管理")
select1.deselect_by_index(1)
select1.deselect_by_value("comment_manage.php?act=list")
sleep(5)
select1.deselect_all()
sleep(1)
select1.select_by_visible_text("    商品类型")
sleep(3)
button = driver.find_element_by_id("btnAdd")
if button.is_enabled():
    button.click()
sleep(5)
list2 = driver.find_element_by_id("menus_navlist")
select2 = Select(list2)
len2 = len(select2.options)
select2.select_by_index(len2-1)
select2.select_by_index(len2-2)
sleep(3)
for i in select2.all_selected_options:
    print(i.text)
print(select2.first_selected_option.text)
driver.quit()

实验13

  1. 打开后台登陆页
  2. 输入管理员姓名admin,输入管理员密码:admin123,输入验证码:0,等待3秒,进行截屏
  3. 点击“进入管理中心“,等待5秒,进行截屏
  4. 对左侧菜单栏进行截屏
  5. 点击“商品列表“,等待3秒,对右侧商品列表区域进行截屏
  6. 点击商品列表中“夏新N7“一行中的查看按钮,等待3秒,切换到新窗口,对新窗口进行截屏
  7. 点击登录按钮,等待3秒,进行截屏
  8. 点击“立即登陆”按钮,等待2秒,进行截屏
  9. 对消息框进行确认
  10. 输入用户名:vip,输入密码:vip
  11. 点击“立即登陆“按钮,等待3秒,进行截屏
  12. 自动刷新页面后,对包含ECSHOP图标在内的最上侧区域进行截屏
  13. 关闭浏览器
from selenium import webdriver
from time import sleep
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://localhost/upload/admin/index.php")
ActionChains(driver).move_to_element(driver.find_element_by_name("username")).click().send_keys("admin").send_keys(Keys.TAB).send_keys("admin123").send_keys(Keys.TAB).send_keys("0").perform()
sleep(3)
driver.save_screenshot(r".\test13_1.png")
driver.find_element_by_class_name("button").click()
sleep(5)
driver.save_screenshot(r".\test13_2.png")
driver.switch_to.frame(driver.find_element_by_id("menu-frame"))
driver.find_element_by_css_selector("body").screenshot(r".\test13_3.png")
driver.find_element_by_css_selector("li.explode:nth-child(1) > ul:nth-child(1) > li:nth-child(1) > a:nth-child(1)").click()
sleep(3)
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element_by_id("main-frame"))
driver.find_element_by_css_selector("body > form:nth-child(6)").screenshot(r".\test13_4.png")
driver.find_element_by_xpath('//span[text()="夏新N7"]/../../td[11]/a/img').click()
sleep(3)
driver.switch_to.window(driver.window_handles[-1])
driver.save_screenshot(r".\test13_5.png")
driver.find_element_by_css_selector("#ECS_MEMBERZONE > a:nth-child(2) > img:nth-child(1)").click()
sleep(3)
driver.save_screenshot(r".\test13_6.png")
driver.find_element_by_css_selector(".us_Submit").click()
driver.switch_to.alert.accept()
ActionChains(driver).move_to_element(driver.find_element_by_name("username")).click().send_keys("vip").send_keys(Keys.TAB).send_keys("vip").perform()
driver.find_element_by_css_selector(".us_Submit").click()
sleep(3)
driver.save_screenshot(r".\test13_7.png")
sleep(3)
driver.find_element_by_css_selector("div.block:nth-child(2)").screenshot(r".\test13_8.png")
driver.quit()

实验14:图片验证码

  1. 打开ECShop前台P806商品详情页http://localhost/upload/goods.php?id=24
  2. 输入E-mail:a@b.com
  3. 输入评论内容:It’s excellent
  4. 识别验证码图片上的内容
  5. 输入正确的验证码
  6. 点击“提交评论“按钮
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import ddddocr
from time import sleep


# 初始化浏览器驱动
driver = webdriver.Firefox()

try:
    # 打开网页
    driver.get("http://localhost/upload/goods.php?id=24")

    # 输入邮箱地址
    email_input = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "email")))
    email_input.send_keys("a@b.com")

    # 输入内容
    content_input = driver.find_element(By.CSS_SELECTOR, '[name="content"]')
    content_input.send_keys("It's excellent")

    # 截取验证码并进行处理
    img = driver.find_element(By.CSS_SELECTOR, '.captcha')
    img.screenshot("captcha.png")

    with open("captcha.png", "rb") as img_file:
        img_bytes = img_file.read()

    ocr = ddddocr.DdddOcr()
    check_code = ocr.classification(img_bytes)

    # 输入验证码
    captcha_input = driver.find_element(By.CSS_SELECTOR, '[name="captcha"]')
    captcha_input.send_keys(check_code)

    # 点击提交按钮
    submit_button = driver.find_element(By.CSS_SELECTOR, 'input.f_r')
    submit_button.click()

finally:
    sleep(15)
    # 关闭浏览器会话
    driver.quit()

实验15:文件上传下载

一、文件上传

  1. 打开ECShop前台登录页
  2. 输入用户名vip、密码vip、点击“立即登陆”,等待5秒
  3. 点击上方“用户中心”,等待3秒
  4. 点击左侧“我的留言”,等待3秒
  5. 输入主题“hello”,输入留言内容“welcome to this world!”
  6. 选择文件c:\temp\777.txt
  7. 点击“提交”

二、文件下载

  1. 打开网页http://sahitest.com/demo/saveAs.htm
  2. 下载testsaveas.zip文件至c:\temp文件夹下
from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
driver.get("http://localhost/upload/user.php")
driver.find_element_by_name("username").send_keys("vip")
driver.find_element_by_name("password").send_keys("vip")
driver.find_element_by_name("submit").click()
sleep(5)
driver.find_element_by_link_text("用户中心").click()
sleep(3)
driver.find_element_by_css_selector(".userMenu > a:nth-child(6)").click()
sleep(3)
driver.find_element_by_name("msg_title").send_keys("hello")
driver.find_element_by_name("msg_content").send_keys("welcome to this world!")
driver.find_element_by_name("message_img").send_keys(r"C:\temp\777.txt")
driver.find_element_by_class_name("bnt_bonus").click()
driver.quit()

settings = webdriver.FirefoxProfile()
settings.set_preference("browser.download.folderList", 2)
settings.set_preference("browser.download.dir", r"C:\temp")
settings.set_preference("browser.download.manager.showWhenStarting", False)
settings.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/zip")
driver = webdriver.Firefox(firefox_profile=settings)
driver.get("http://sahitest.com/demo/saveAs.htm")
driver.find_element_by_css_selector("body > a:nth-child(2)").click()
sleep(3)
driver.quit()