(Python + Selenium4)Web自动化测试自学Day2之动手尝试

目录


文章声明⭐⭐⭐

  1. 该文章为我(有编程语言基础,非编程小白)的 Python Selenium4 Web自动化测试自学笔记
  2. 知识来源为 B站UP主(软件测试老白)的Python Selenium4课程视频,归纳为自己的语言与理解记录于此并加以实践
  3. 不出意外的话,我大抵会 持续更新
  4. 想要了解前端开发(技术栈大致有:Vue2/3、微信小程序、uniapp、HarmonyOS、NodeJS、Typescript)与Python的小伙伴,可以关注我!谢谢大家!

让我们开始今天的学习吧!

小试牛刀

首先我们先来做个小案例,要求如下:

  • 进入iview官网(https://www.iviewui.com/)
  • 点击体验免费组件库
  • 依次点击组件-表单-Radio,选择Windows
  • 再点击checkbox,把未勾选上的复选框都勾选上

代码如下:

python 复制代码
# 相关导入
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By

if __name__ == '__main__':
    # 实例化浏览器对象
    web = Chrome()
    # 全屏
    web.maximize_window()
    # 使用get方法进入网站
    web.get('https://www.iviewui.com')
    time.sleep(1)
    # 点击体验免费组件库
    web.find_element(By.XPATH, '//a[@href="/view-ui-plus/"]').click()
    time.sleep(1)
    # 点击组件
    web.find_element(By.XPATH, '//span[text()="组件"]/../..').click()
    time.sleep(1)
    # 点击表单
    web.find_element(By.XPATH, '//span[text()="表单"]/../..').click()
    time.sleep(1)
    # 点击Radio
    web.find_element(By.XPATH, '//a[@href="/view-ui-plus/component/form/radio"]').click()
    time.sleep(1)
    # 点击Windows单选框
    web.find_element(By.XPATH, '//span[text()="Windows"]/preceding-sibling::span/input').click()
    time.sleep(1)
    # 点击checkbox
    web.find_element(By.XPATH, '//a[@href="/view-ui-plus/component/form/checkbox"]').click()
    time.sleep(1)
    # 依次勾选上那些未勾选上的复选框
    web.find_element(By.XPATH, '//span[text()="Twitter"]').click()
    time.sleep(1)
    web.find_element(By.XPATH, '//span[text()="Snapchat"]').click()
    time.sleep(1)
    web.find_element(By.XPATH, '//span[text()="香蕉"]').click()
    time.sleep(1)
    web.find_element(By.XPATH, '//span[text()="西瓜"]').click()
    time.sleep(1)
    # 停留五秒
    time.sleep(5)
    # 关闭浏览器,selenium4加不加close方法都会关闭浏览器
    web.close()

关于select标签

python 复制代码
# 相关导入
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

if __name__ == '__main__':
    # 实例化浏览器对象
    web = Chrome()
    # 全屏
    web.maximize_window()
    # 使用get方法进入网站
    web.get('https://sahitest.com/demo/selectTest.htm')
    time.sleep(1)
    # 选取select标签
    my_select = Select(web.find_element(By.ID, 'testInputEvent'))
    # 根据索引选取值
    # my_select.select_by_index(3)
    # 根据value选取值
    # my_select.select_by_value('value3')
    # 根据用户可见的选项内容选取值
    # my_select.select_by_visible_text('Option 2')

    # 停留五秒
    time.sleep(5)
    # 关闭浏览器,selenium4加不加close方法都会关闭浏览器
    web.close()

关于弹窗

只有一个点击按钮的弹窗

python 复制代码
# 相关导入
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By

if __name__ == '__main__':
    # 实例化浏览器对象
    web = Chrome()
    # 全屏
    web.maximize_window()
    # 使用get方法进入网站
    web.get('https://sahitest.com/demo/alertTest.htm')
    time.sleep(1)
    # 点击按钮弹出弹窗
    web.find_element(By.NAME, 'b1').click()
    time.sleep(1)
    # 输出弹窗内容
    print(web.switch_to.alert.text)
    # 点击弹窗的确认按钮
    web.switch_to.alert.accept()
    # 停留五秒
    time.sleep(5)
    # 关闭浏览器,selenium4加不加close方法都会关闭浏览器
    web.close()

需要确认的弹窗

python 复制代码
# 相关导入
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By

if __name__ == '__main__':
    # 实例化浏览器对象
    web = Chrome()
    # 全屏
    web.maximize_window()
    # 使用get方法进入网站
    web.get('https://sahitest.com/demo/confirmTest.htm')
    time.sleep(1)
    # 点击按钮弹出弹窗
    web.find_element(By.NAME, 'b1').click()
    time.sleep(1)
    # 输出弹窗内容
    print(web.switch_to.alert.text)
    # 点击弹窗的确认按钮
    # web.switch_to.alert.accept()
    # 点击弹窗的取消按钮
    web.switch_to.alert.dismiss()
    # 停留五秒
    time.sleep(5)
    # 关闭浏览器,selenium4加不加close方法都会关闭浏览器
    web.close()

用户可以输入的弹窗

python 复制代码
# 相关导入
import time
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By

if __name__ == '__main__':
    # 实例化浏览器对象
    web = Chrome()
    # 全屏
    web.maximize_window()
    # 使用get方法进入网站
    web.get('https://sahitest.com/demo/promptTest.htm')
    time.sleep(1)
    # 点击按钮弹出弹窗
    web.find_element(By.NAME, 'b1').click()
    time.sleep(1)
    # 输入内容
    web.switch_to.alert.send_keys('我输出了内容')
    # 点击弹窗的确认按钮
    web.switch_to.alert.accept()
    # 点击弹窗的取消按钮
    # web.switch_to.alert.dismiss()
    # 停留五秒
    time.sleep(5)
    # 关闭浏览器,selenium4加不加close方法都会关闭浏览器
    web.close()
相关推荐
言乐617 小时前
Python实现建造微服务商城后台
开发语言·python·算法·微服务·架构
fenglllle17 小时前
chromadb emmbedding 向量检索
人工智能·python·embedding
cui_ruicheng17 小时前
Python从入门到实战(六):非序列容器
开发语言·python
西西学代码17 小时前
Flutter---底部导航栏(2)
开发语言·javascript·flutter
legendary_16317 小时前
SINK芯片:Type-C统一供电时代的小家电核心方案
c语言·开发语言·人工智能·智能手机
momo17 小时前
JAVA基础知识
java·开发语言
凯瑟琳.奥古斯特18 小时前
力扣1013三等分解法与C++实现
开发语言·c++·算法·leetcode·职场和发展
飞猪~18 小时前
Langchain python版本 LLM 重要函数invoke,ainvoke,stream,astream,batch,abatch
python·langchain·batch
沙蒿同学18 小时前
当古诗词遇上 AI:从 38 万句诗词中取一个好名字
python·算法·架构
xxie12379419 小时前
Python装饰器与语法糖
开发语言·python