当前位置:网站首页>Automated testing ----- selenium (II)
Automated testing ----- selenium (II)
2022-07-27 21:05:00 【Pinellia and cool】
Catalog
Positioning of multi-storey frame
Positioning of multi-layer windows
alert Handling of pop-up frames
Locate a set of elements
webdriver It's easy to use findElement Method to locate a specific object , But sometimes we need to locate a set of objects , You need to use findElements Method .
Application scenarios :
- Batch operation objects , For example, all the checkbox All hooked
- First get a set of objects , Then filter out some objects that need to be located in this group of objects . For example, locate all the information on the page checkbox, Then choose the last
#coding=utf-8
from selenium import webdriver
import time
import os
dr = webdriver.Chrome()
# Open local path 'file:///'+os.path.abspath(' File absolute path ')
file_path = 'file:///' + os.path.abspath('checkbox.html')
dr.get(file_path)
# Select all on the page input, Then filter out all checkbox And check
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() Get attribute value Positioning of multi-storey frame
Position a frame frame :switch_to.frame(name_or_id_or_frame_element)
adopt frame Of id perhaps name perhaps frame Other self-contained attributes to locate the framework , here switch_to.frame() Switch the currently located subject frame in .switch_to.default_content: from frame Jump out of the page embedded in , Jump back to the outermost default page .
#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)
# Find it first ifrome1(id = f1)
browser.switch_to.frame("f1")
# Then find the... Below it ifrome2(id =f2)
browser.switch_to.frame("f2")
# Now you can operate the elements normally
browser.find_element_by_id("kw").send_keys("selenium")
browser.find_element_by_id("su").click()
time.sleep(3)
browser.quit()Positioning of multi-layer windows
Locate a window window:switch_to.window(name_or_id_or_frame_element)
Methods and switch_to.fram() equally
Hierarchical positioning
Sometimes the elements we need to locate are not directly displayed on the page , Instead, the elements of the page need to be displayed after a series of operations , At this time, we need to position layer by layer .
Ideas :
First click to display 1 A drop-down menu , Then navigate to the... Where the drop-down menu is located ul, Reposition this ul Under a specific 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)
# Click on Link1 link ( Pop up the drop-down list )
dr.find_element_by_link_text('Link1').click()
# Find... Under the father element link by Action Child elements
menu = dr.find_element_by_id('dropdown1').find_element_by_link_text('Action')
# Position the mouse over the child element
webdriver.ActionChains(dr).move_to_element(menu).perform()
time.sleep(2)
dr.quit()Drop down box processing
Drop down box is one of the most common page elements , For general elements , We only need to position once , But the content in the drop-down box needs to be positioned twice , First navigate to the drop-down box and operate on the drop-down box , Then navigate to the options in the drop-down box .
#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)
# Go to the drop-down box first
m=driver.find_element_by_id("ShippingMethod")
# Then click the option under the drop-down box
m.find_element_by_xpath("//option[@value='10.69']").click()
time.sleep(3)
driver.quit()alert Handling of pop-up frames
# -*- 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)
# Click the link to pop up alert
dr.find_element_by_id('tooltip').click()
sleep(2)
# Get the operation handle of the pop-up box
alert = dr.switch_to.alert()
# close alert
alert.accept()
sleep(2)
dr.quit()
# Accept warning messages
alert = dr.switch_to.alert()
alert.accept()
# Get text information and print
alert = dr.switch_to.alert()
print alert.text
# Cancel the dialog ( If any )
alert = dr.switch_to.alert()
alert.dismiss()
# The input values
alert = dr.switch_to.alert()
alert.send_keys("hello word")
div Block processing
If there are many page elements , When an element cannot be accurately located by using its attributes , We can locate the element first div block , Then locate this element .
div1=driver.find_element_by_class_name("midal-body")
div1.find_element_by_link_text("click me").click()
time.sleep(3)
Upload file operation
To upload a file, just locate the upload button , adopt send_keys Just add the local file path . Both absolute and relative paths are OK , The key is that the uploaded file exists .
driver.find_element_by_name("file").send_keys(" File absolute path ")边栏推荐
- The variable "lattice" or class "lattice.latticeeasy" (matlab) is not defined
- MySQL design optimization generates columns
- One article to understand pychar shortcut key
- Things about stack migration
- 程序中的地址如何转换?
- [deep learning] pytoch torch Autograd automatic differential engine
- [numpy] broadcast mechanism
- Hexagon_ V65_ Programmers_ Reference_ Manual(7)
- How to realize document collaboration?
- 《SRE:Google运维解密》读后有感
猜你喜欢
随机推荐
R语言使用epiDisplay包的power.for.2p函数进行效用分析 ( 效能分析、Power analysis)、给定两个样本的比例值(proportions)、样本量计算效用值
How to talk to CIO / CTO
Force deduction solution summary 592 fraction addition and subtraction
如何解决tp6控制器不存在:app\controller\Index
A lock faster than read-write lock. Don't get to know it quickly
The variable "lattice" or class "lattice.latticeeasy" (matlab) is not defined
认识网络模型网络模型概述
实名认证在文旅出行行业的应用场景有哪些?
NATAPP内网穿透工具外网访问个人项目
认识网络模型数据的封装和解封装
认识网络模型TCPIP模型
Recommend a powerful search tool listary
redis cook book.notes.
[numpy] array index and slice
最新版web漏洞扫描工具AppScan\AWVS\Xray安装及使用教程
Sscanf caused the address to be out of bounds
[Numpy] 广播机制(Broadcast)
Arduino development (II)_ RGB light control method based on Arduino uno development board
获取委托中注册的方法
SQL coding bug








