当前位置:网站首页>自动化测试----selenium(二)
自动化测试----selenium(二)
2022-07-27 18:28:00 【半夏而凉】
目录
定位一组元素
webdriver 可以很方便的使用findElement 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,这时候就需要使用findElements 方法。
应用场景:
- 批量操作对象,比如将页面上所有的checkbox 都勾上
- 先获取一组对象,再在这组对象中过滤出需要具体定位的一些对象。比如定位出页面上所有的checkbox,然后选择最后一个
#coding=utf-8
from selenium import webdriver
import time
import os
dr = webdriver.Chrome()
#打开本地路径 'file:///'+os.path.abspath('文件绝对路径')
file_path = 'file:///' + os.path.abspath('checkbox.html')
dr.get(file_path)
# 选择页面上所有的input,然后从中过滤出所有的checkbox 并勾选之
inputs = dr.find_elements_by_tag_name('input')
for input in inputs:
if input.get_attribute('type') == 'checkbox':
input.click()
time.sleep(2)
dr.quit()
#get_attribute() 获得属性值多层框架的定位
定位一个框架frame :switch_to.frame(name_or_id_or_frame_element)
通过frame的id或者name或者frame自带的其它属性来定位框架,这里switch_to.frame()把当前定位的主体切换了frame里。switch_to.default_content:从frame中嵌入的页面里跳出,跳回到最外面的默认页面中。
#coding=utf-8
from selenium import webdriver
import time
import os
browser = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('frame.html')
browser.get(file_path)
browser.implicitly_wait(30)
#先找到到ifrome1(id = f1)
browser.switch_to.frame("f1")
#再找到其下面的ifrome2(id =f2)
browser.switch_to.frame("f2")
#下面就可以正常的操作元素了
browser.find_element_by_id("kw").send_keys("selenium")
browser.find_element_by_id("su").click()
time.sleep(3)
browser.quit()多层窗口的定位
定位一个窗口window:switch_to.window(name_or_id_or_frame_element)
方法与switch_to.fram()一样
层级定位
有时候我们需要定位的元素没有直接在页面展示,而是需要对页面的元素经过一系列操作之后才展示出来,这个时候我们就需要一层层去定位.
思路:
先点击显示出1个下拉菜单,然后再定位到该下拉菜单所在的ul,再定位这个ul下的某个具体的link。
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
import os
dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('level_locate.html')
dr.get(file_path)
#点击Link1链接(弹出下拉列表)
dr.find_element_by_link_text('Link1').click()
#在父亲元件下找到link 为Action 的子元素
menu = dr.find_element_by_id('dropdown1').find_element_by_link_text('Action')
#鼠标定位到子元素上
webdriver.ActionChains(dr).move_to_element(menu).perform()
time.sleep(2)
dr.quit()下拉框处理
下拉框是我们最常见的一种页面元素,对于一般的元素,我们只需要一次就定位,但下拉框里的内容需要进行两次定位,先定位到下拉框对下拉框进行操作后,再定位到下拉框内里的选项。
#coding=utf-8
from selenium import webdriver
import os,time
driver= webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('drop_down.html')
driver.get(file_path)
time.sleep(2)
#先定位到下拉框
m=driver.find_element_by_id("ShippingMethod")
#再点击下拉框下的选项
m.find_element_by_xpath("//option[@value='10.69']").click()
time.sleep(3)
driver.quit()alert弹框的处理
# -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep
import os
dr = webdriver.Chrome()
file_path = 'file:///' + os.path.abspath('alert.html')
dr.get(file_path)
# 点击链接弹出alert
dr.find_element_by_id('tooltip').click()
sleep(2)
#获得弹出框的操作句柄
alert = dr.switch_to.alert()
#关闭alert
alert.accept()
sleep(2)
dr.quit()
#接受警告信息
alert = dr.switch_to.alert()
alert.accept()
#得到文本信息打印
alert = dr.switch_to.alert()
print alert.text
#取消对话框(如果有的话)
alert = dr.switch_to.alert()
alert.dismiss()
#输入值
alert = dr.switch_to.alert()
alert.send_keys("hello word")
div块的处理
如果页面元素比较多,利用元素的属性无法准确的定位这个元素的时候,我们可以先定位元素所在的div块,再去定位这个元素。
div1=driver.find_element_by_class_name("midal-body")
div1.find_element_by_link_text("click me").click()
time.sleep(3)
上传文件操作
上传文件只要定位上传按钮,通过send_keys 添加本地文件路径就可以了。绝对路径和相对路径都可以,关键是上传的文件存在。
driver.find_element_by_name("file").send_keys("文件绝对路径")边栏推荐
- After working for bytek for two years, he got 15 offers at one go
- 认识传输介质通信方式
- The variable "lattice" or class "lattice.latticeeasy" (matlab) is not defined
- 程序放在哪儿?
- UE5使用DLSS(超级采样)提升场景的 FPS 远离卡顿的优化方案
- Users and permissions revoke user permissions
- [numpy] array properties
- Flask Mdict builds online MDICT Dictionary Service
- Express WEB服务器的简单使用
- IOU 目标跟踪其二:VIOU Tracker
猜你喜欢

Why are TS slice files generated when easycvr platform turns off video recording?

Hcip day 5

命令行 PDF 转换器:::fCoder 2PDF

Using dataX to realize efficient synchronization of MySQL data

Write bootloader from 0

Xdc 2022 Intel technology special session: Intel Software and hardware technology builds the cornerstone of cloud computing architecture

Automatic test solution based on ATX

adb shell ls /system/bin(索引表)

【Dart】一门为跨端开发而生的编程语言

People call this software testing engineer. You're just making a living (with HR interview Dictionary)
随机推荐
金仓数据库 KingbaseES异构数据库移植指南 (3. KingbaseES移植能力支撑体系)
Onion group joined hands with oceanbase to realize distributed upgrading, and global data has achieved cross cloud integration for the first time
RK3399平台开发系列讲解(进程篇)15.36、理解进程和协程
金仓数据库 KingbaseES 异构数据库移植指南 (4. 应用迁移流程)
[deep learning] pytoch tensor
Can tonghuashun open an account on weekends? Is it safe to open an account
Hexagon_V65_Programmers_Reference_Manual(6)
2022-07-19 advanced network engineering (XX) BGP route optimization, route optimization analysis one by one
knife4j通过js动态刷新全局参数
When adding RTSP devices to easycvr platform, what is the reason for the phenomenon that they are all connected by TCP?
你了解数据同步吗?
[efficiency] abandon notepad++, this open source substitute is more awesome!
认识网络模型网络模型概述
User login switching case
关于栈迁移的那些事儿
用户登录切换案例
MySQL design optimization generates columns
《SRE:Google运维解密》读后有感
Technology blog and tutorial
[Numpy] 广播机制(Broadcast)