当前位置:网站首页>Summary of webdriver API for web automated testing
Summary of webdriver API for web automated testing
2022-06-13 04:13:00 【YZL40514131】
One 、 Control browser
1、driver.set_window_size(value,value): Set the size of the browser , Unit is pixel ;
2、driver.back(): Control browser backward
3、driver.forward(): Control the browser's progress
4、driver.refresh(): Refresh the browser
from selenium import webdriver
import time
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com')
#todo Set the size of the browser
driver.set_window_size(2000,2000) # Unit is pixel
input_el=driver.find_element_by_xpath('//input[@id="kw"]')
input_el.send_keys('kobe')
butten_el=driver.find_element_by_xpath('//input[@type="submit"]').click()
time.sleep(2)
#todo Control browser backward
driver.back()
time.sleep(2)
#todo Control the browser's progress
driver.forward()
time.sleep(2)
#todo Refresh the browser
driver.refresh()
Two 、WebDriver Common methods
clear() Clear text
send_keys(value) Keyboard entry
click() Click on the element
submit() Submit Form
size Returns the size of the element
text Get the text of the element
get_attribute(name) Get attribute value
is_displayed() Set whether the element is visible to the user
from selenium import webdriver
import time
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com')
input_el=driver.find_element_by_xpath('//input[@id="kw"]')
input_el.send_keys('kobe')
time.sleep(2)
#todo Clear text
input_el.clear()
input_el.send_keys('curry')
#todo Text box submit
input_el.submit()
from selenium import webdriver
import time
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com')
# Get the size of the input box
input_el=driver.find_element_by_xpath('//input[@id="kw"]')
print(input_el.size)
#todo Get text information
el1=driver.find_element_by_xpath('//a[@class="mnav c-font-normal c-color-t"][2]')
print(el1.text)
#todo obtain target The attribute value
print(el1.get_attribute('target'))
#todo Returns the element visible
print(el1.is_displayed())
3、 ... and 、 Get validation information
driver.title: Get the page title
driver.current_url: Access to web pages url
from selenium import webdriver
import time
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com')
print(' Before searching —— Current page title and url')
print(driver.title)
print(driver.current_url)
input_el=driver.find_element_by_xpath('//input[@id="kw"]')
input_el.send_keys('kobe')
butten_el=driver.find_element_by_xpath('//input[@type="submit"]').click()
print(' After search —— Current page title and url')
print(driver.title)
print(driver.current_url)
Four 、 Explicit wait
locator=(‘xpath’,‘//input[@id=“kw1”]’)
webdriverwait=WebDriverWait(driver,5,0.5)
With the right until Method Determines whether an element is visible , Element visible continue down
el=webdriverwait.until(expected_conditions.visibility_of_element_located(locator))
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
import time
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com')
# Set linear wait
webdriverwait=WebDriverWait(driver,5,0.5)
# With the right until Method Determines whether an element is visible , Element visible continue down
locator=('xpath','//input[@id="kw1"]')
el=webdriverwait.until(expected_conditions.visibility_of_element_located(locator))
el.send_keys('kobe')
el1=driver.find_element_by_xpath('//input[@type="submit"]').click()
Custom explicit wait
from selenium import webdriver
import time
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com')
print(' Output a time :',time.ctime())
# Use a loop to determine whether the positioned element exists
for i in range(10):
try:
element=driver.find_element_by_xpath('//input[@id="kw1"]')
if element.is_displayed():
break
except:
pass
time.sleep(1)
else:
print('Timeout')
print(' The waiting time ',time.ctime())
5、 ... and 、 An implicit wait
driver.implicitly_wait(10) # The unit is seconds
6、 ... and 、 Locate a set of elements
Get a set of elements
el_list=driver.find_elements_by_xpath(‘//div[@class=“name-box”]/a’)
from selenium import webdriver
import time
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://qidian.com/rank/')
# Get a set of elements
el_list=driver.find_elements_by_xpath('//div[@class="name-box"]/a')
print(len(el_list))
# Traverse the list
for el in el_list:
print(el.text)
Execution results
The star catcher will seize the star river
7 Base No
Dr. Chen , Don't talk !
Please kill the demon
The start is more serious , I scared the police across the street !
Matchmaking , I don't agree with anyone
once , I want to be a good person
Daming : I , Teach the carpenter the Emperor
As Superman's brother, I became a native of China
Spirit Walker
Mink Street 13 Number
The game is too real
Unscientific Royal beast
My father Han Gaozu
The other side of deep space
Choose a day to soar
My attribute is cultivating life
I have not been a big man for many years
Choose a day to soar
The naming of the night
Dr. Chen , Don't talk !
My attribute is cultivating life
I will never die in the immortal cultivation world
The start is more serious , I scared the police across the street !
Space pro
The other side of deep space
What is hexagon fighting wild
The naming of the night
My father Han Gaozu
Choose a day to soar
Mink Street 13 Number
The game is too real
Taoist weird immortal
Only in Mahayana can there be a counter attack system
My attribute is cultivating life
Unscientific Royal beast
Escape from the famine to Wuxia
Meiman : My Warhammer Simulator
Cao Cao crosses Wu Dalang
For twenty years , I entered the thriller game
What do you mean Carry Type on a single ah
The price is reversed : I killed immortals in Dazhou
Start from the body skill and cross the heavens
Film and television life starting from Ode to joy
This warrior is too dangerous
7、 ... and 、 Multi-window switching
Get the handle of the current window :search_windows=driver.current_window_handle
Get the handle of all current windows :all_handles=driver.window_handles
Go back to the previous window :driver.switch_to.window(search_windows)
from selenium import webdriver
import time
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://baidu.com')
driver.implicitly_wait(10)
# Get the handle of Baidu search window
search_windows=driver.current_window_handle
driver.find_element_by_xpath('//a[text()="hao123"]').click()
time.sleep(2)
# Get the handle of all current windows
all_handles=driver.window_handles
print(all_handles)
# Switch to hao123 page
for handle in all_handles:
if handle!=search_windows:
driver.switch_to.window(handle)
print(driver.title)
driver.find_element_by_xpath('//a[text()=" NetEase "]').click()
# Go back to the search window
driver.switch_to.window(search_windows)
8、 ... and 、 Handling of warning box
Get warning box : alert=driver.switch_to.alert
alert.text: Get the text information of the warning box
Receive warning box : alert.accept()
from selenium import webdriver
import time
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://baidu.com')
driver.implicitly_wait(10)
driver.find_element_by_xpath('//span[text()=" Set up "]').click()
driver.find_element_by_xpath('//span[text()=" Search settings "]').click()
# Locate the save settings element
driver.find_element_by_xpath('//a[text()=" Save settings "]').click()
# Get warning box
alert=driver.switch_to.alert
print(alert.text) # Get the text information of the warning box
# Receive warning box
alert.accept()
Nine 、 Handling of drop-down boxes
from selenium import webdriver
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.hao123.com/?src=from_pc')
driver.implicitly_wait(10)
select_el=driver.find_element_by_xpath('//span[text()=" Webpage "]').click()
# according to value choice
Select(select_el).select_by_value('image')
time.sleep(1)
# Select according to text
Select(select_el).select_by_visible_text(' Map ')
time.sleep(1)
# Choose... According to the index
Select(select_el).select_by_index(0)
time.sleep(1)
Ten 、 Download the file
from selenium import webdriver
import time
import os
# Configuration information
options=webdriver.ChromeOptions()
prefs={
'profile_default_content_settings.popups':0,# Do not pop up the download window
'download.default_directory':os.getcwd()
}
options.add_experimental_option('prefs',prefs)
# Create a Google browser driver
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe",chrome_options=options)
driver.implicitly_wait(10)
# Open the web page
driver.get('https://www.python.org/downloads/')
# Locate web elements , Click download
driver.find_element_by_xpath('//p[@class="download-buttons"]//a[@href="https://www.python.org/ftp/python/3.10.5/python-3.10.5-amd64.exe"]').click()
11、 ... and 、 operation Cookie
Get all cookies Information : driver.get_cookies()
add to cookies Information driver.add_cookie({‘name’:‘yzl’,‘value’:‘yzl111’})
Delete cookie driver.delete_cookie(‘yzl’)
Delete all cookie Information driver.delete_all_cookies()
from selenium import webdriver
import time
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com')
driver.implicitly_wait(10)
# Get all cookies Information
print(driver.get_cookies())
# add to cookies Information
driver.add_cookie({
'name':'yzl','value':'yzl111'})
# Traverse all cookie Information
for item in driver.get_cookies():
print(item)
# Delete cookie
driver.delete_cookie('yzl')
# Delete all cookie Information
driver.delete_all_cookies()
print(driver.get_cookies())
Twelve 、 Window screenshot and close
Screenshot : driver.save_screenshot(‘baidu.png’)
from selenium import webdriver
import time
driver=webdriver.Chrome("C:\Program Files\Google\Chrome\Application\chromedriver.exe")
driver.get('https://www.baidu.com')
driver.implicitly_wait(10)
time.sleep(2)
# Start the screenshot operation
driver.save_screenshot('baidu.png')
time.sleep(2)
driver.close()

边栏推荐
- Google Chrome browser reports an error: net:: err_ BLOCKED_ BY_ CLIENT
- Lambda end operation find and match findany
- Common array methods in JS (map, filter, foreach, reduce)
- 120. 三角形最小路径和-动态规划
- Introduction and use of ES6
- Lambda end operation find and match findfirst
- Binocular vision -- creating an "optimal solution" for outdoor obstacle avoidance
- Uni app enables pull-up loading and pull-down refresh (pull-down with animation)
- 5G China unicom AP:B SMS ASCII 转码要求
- [Yugong series] June 2022 Net architecture class 081 API customization task of distributed middleware schedulemaster
猜你喜欢

基于PHP的轻量数码商城系统

【自动化测试】关于unittest你需要知道的事

Goframe day 4

单片机:A/D 差分输入信号

Single chip microcomputer: a/d differential input signal

leetcode.1 --- 两数之和

Single chip microcomputer: basic concepts of a/d and d/a

On the value of line height

高等数学(第七版)同济大学 习题1-3 个人解答

10 minutes to thoroughly understand how to configure sub domain names to deploy multiple projects
随机推荐
基于DE2-115平台的VGA显示
7-289 tag count (300 points)
Single chip microcomputer: MODBUS multi computer communication program design
How to use debounce in lodash to realize anti shake
EGO planner论文翻译
单片机:RS485 通信与 Modbus 协议
Lambda termination operation Max & min
Lambda end operation find and match findfirst
Lambda termination operation find and match anymatch
How can a sweeping robot avoid obstacles without "mental retardation"? Analysis of five mainstream obstacle avoidance techniques
Principle and control program of single chip microcomputer serial port communication
Sword finger offer II 022 Entry node of a link in a linked list
Zoom and move the H5 part of the mobile end
Difference between OKR and KPI
Data analysis report
微信扫描二维码无法下载文件的解决办法
Introduction to MCU peripherals: temperature sensor DS18B20
单片机信号发生器程序
Example of try catch finally execution sequence
R: Airline customer value analysis practice