当前位置:网站首页>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()
边栏推荐
- 基于DE2-115平台的VGA显示
- Interpretation of mobile phone private charging protocol
- Answer private message @ Tiantian Wx //2022-6-12 C language 51 single chip microcomputer led analog traffic light
- Et framework -22 creating serverinfo entities and events
- 单片机:I2C通信协议讲解
- Lightweight digital mall system based on PHP
- R: Employee turnover forecast practice
- 单片机:红外遥控通信原理
- 5g China Unicom ap:b SMS ASCII transcoding requirements
- Google Chrome browser reports an error: net:: err_ BLOCKED_ BY_ CLIENT
猜你喜欢
单片机:红外遥控通信原理
微信扫描二维码无法下载文件的解决办法
单片机:A/D 差分输入信号
Difference between OKR and KPI
单片机:Modbus 通信协议介绍
Use the visual studio code terminal to execute the command, and the prompt "because running scripts is prohibited on this system" will give an error
Promise combined with await
单片机:PCF8591 应用程序
Solution to failure to download files by wechat scanning QR code
【LeetCode】860. Change with lemonade (2 brushes for wrong questions)
随机推荐
knife4j aggregation 2.0.9支持路由文档自动刷新
[kubernetes series] pod chapter actual operation
手机私有充电协议解读
Answer private message @ Tiantian Wx //2022-6-12 C language 51 single chip microcomputer led analog traffic light
解答私信@田田WX //2022-6-12 C语言 51单片机LED模拟交通灯
Understand the pseudo static configuration to solve the 404 problem of refreshing the page of the deployment project
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-2 personal solutions
Redis persistence mode AOF and RDB
Lambda termination operation find and match anymatch
Lambda end operation find and match allmatch
Hugo 博客搭建教程
[test development] fundamentals of software testing
单片机:A/D 和 D/A 的基本概念
Lambda end operation find and match findany
The WebView case of flutter
5G China unicom 直放站 网管协议 实时性要求
LVS 4 - tier Load Balancing Cluster (3) Cluster Function Classification - HPC
单片机:NEC 协议红外遥控器
单片机:I2C通信协议讲解
UE4 learning notes - functions of terrain tool