当前位置:网站首页>Web automation: web control interaction / multi window processing / Web page frame
Web automation: web control interaction / multi window processing / Web page frame
2022-06-24 21:05:00 【gchh_ spring】
order :web automation , In addition to locating web page elements , After locating the element , Generally, a series of operations are required , The most common is to click click, It also includes input files 、 Right click on the , Page slide , Form operation, etc
selenium webdriver api Guidance document :
https://selenium-python.readthedocs.io/api.html
selenium Operation interface provided :
ActionChains: perform PC Mouse click on the end , double-click , Right click , Drag and drop
TouchActions: simulation PC And mobile click , slide , Drag and drop , Multi touch and other gesture operations
ActionChains usage :
Contains some basic operations :
click(on_element=None): Simulate left click , If it doesn't come in element, Then click the current position of the mouse
click_and_hold(on_element=None): Clicking does not release
context_click(on_element=None): Simulate right clicking
double_click(on_element=None): Simulate double click
drag_and_drop(source, target): Drag and move to another position to drop , Pass in two elements , The first element position and the second element position
key_down(value, element=None)
key_up(value, element=None)
ActionChains Execution principle
call ActionChains Method time , Not immediately , Instead, all the operations , Put them in a queue in order , When calling perform() When the method is used , Will be executed in this way Basic usage : 1、 Generate an action action = ActionChains(driver) 2、 Add action method 1 action. Method 1 3、 Add action method 2 action. Method 2 4、 call action.perform() Method execution How to write it : 1、 Chain writing ActionChains(driver).move_to_element(element).click(element).perform() 2、 Distribution writing action = ActionChains(driver) action.move_to_element(element) action.click(element) action.perform()
1、 Click on the simulation 、 Right click 、 Double click and so on
element_click = self.driver.find_element_by_xpath('xpath')
element_doubleclick = self.driver.find_element_by_xpath('xpath')
element_rightclick = self.driver.find_element_by_xpath('xpath')
action = ActionChains(self.driver)
action.click(element_click)
action.double_click(element_doubleclick)
action.context_click(element_rightclick)
sleep(3)
action.perform()2、 Simulate the mouse moving over an element
def test_movetoelement(self):
self.driver.get("https://www.baidu.com/")
self.driver.maximize_window()
element = self.driver.find_element_by_xpath('//*[@id="u1"]/span')
action = ActionChains(self.driver)
action.move_to_element(element)
action.perform()
sleep(5)3、 Analog buttons
There are many ways to simulate key pressing :1、 use win32api To achieve 2、 It works SendKeys To achieve 3、 It works selenium Of webelement Object's send_keys() Method realization
4、ActionChains Class also provides several methods to simulate keys
ActionChains Usage introduction
1、action = ActionChains(self.driver)
2、action.send_keys(Keys.BACK_SPACE)
3、 perhaps action.key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL)
4、action.perform() self.driver.get()
element = self.driver.find_element_by_xpath("")
element.click()
action = ActionChains(self.driver)
action.send_keys("username").pause(1)
action.send_keys(Keys.SPACE).pause(1)
action.send_keys("tom").pause(1)
action.send_keys(Keys.BACK_SPACE).perform()TouchActions usage :
Official document :
TouchAction Similar to ActionChains,ActionChains Just for PC A series of operations simulated by the client program mouse , Yes h5 Page operation is invalid TouchAction It can be done to h5 Page operation , You can click 、 slide 、 multi-touch 、 And various operations of simulating gestures Gesture control : tap--- Click... On the specified element double_tap--- Double click on the specified element tap_and_hold--- Click on the specified element without releasing move--- Gesture movement specifies the offset ( Don't release ) release--- Release gesture scroll--- Gesture click and scroll scroll_form_element--- Start with an element position, click and scroll ( Sliding down is a negative number , Sliding up is a positive number ) long_press--- Long press element flick--- Gesture slide flick_element--- Start the gesture slide from an element position ( Sliding up is a negative number , Slide down to a positive number ) perform--- perform
class TestTouchActions:
def setup(self):
option = webdriver.ChromeOptions()
option.add_experimental_option('w3c', False)
self.driver = webdriver.Chrome(options=option)
self.driver.implicitly_wait(5)
self.driver.maximize_window()
def teardown(self):
self.driver.quit()
def test_touchactions_scrollbutton(self):
'''
Open Baidu web page
Type in the search box ‘selenium’
Slide to bottom , Click the next button
close chorme browser
'''
self.driver.get("https://www.baidu.com")
ele = self.driver.find_element_by_id('kw').send_keys('selenium')
ele_su = self.driver.find_element_by_id('su')
action = TouchActions(self.driver)
action.tap(ele_su)
action.perform()
# Slide to bottom
action.scroll_from_element(ele_su, 0, 10000).perform()
sleep(2)Form action
A form is an area that contains form elements , Is to allow users to ( Such as text field 、 The drop-down list 、 Radio buttons 、 Check boxes, etc ) Elements of input information
Forms use form labels (<form>) Definition , for example :<form><input/></form>
Steps to manipulate form elements :
1、 Navigate to the form element
2、 Operational elements : Empty , Enter or click... Etc
Multi window processing
When we click a button on a web page , If you open a new web page window to realize page Jump , Then you need to operate on the new page , How to deal with this situation
Want to operate on a new page , You have to switch windows first , Get the unique identity of the window, represented by a handle , So just switch the handle , You can operate flexibly on multiple pages
Multi window processing method :
1、 Get the current window handle first :driver.current_window_handle
2、 Then get the handle of all windows :driver.window_handles
3、 Determine whether it is the window you want to operate , If it is , You can operate the window , If not , Jump to another window :driver.switch_to.window, Operate on another window
Multi window switching case : Open Baidu page , Click login , Click Register now in the pop-up box ( Open a new page , Enter... In the user name username), Return to the login page , Click the user name to log in
def test_windows(self):
self.driver.get("https://www.baidu.com/")
self.driver.find_element_by_xpath('//*[@id="u1"]/a').click()
print(self.driver.current_window_handle)
self.driver.find_element_by_link_text(' Register now ').click()
print(self.driver.window_handles)
print(self.driver.current_window_handle)
windows = self.driver.window_handles
self.driver.switch_to.window(windows[1])
self.driver.find_element_by_id("TANGRAM__PSP_4__userName").send_keys("username")
sleep(3)
self.driver.switch_to.window(windows[0])
self.driver.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn').click()
sleep(3)Webpage frame Handle
When a web page has more than one html Page composition , For example, web pages are embedded , Then there will be more frame, How to deal with this kind of scene
stay web In automation , If an element cannot be located , So it's probably in iframe in
What is? frame:frame yes html The framework in , stay html in , The so-called framework is that it can display more than one page in the same browser , be based on html Framework , It is also divided into vertical frame and horizontal frame (cols,rows)
frame classification :
frame Label contains frameset、frame、iframe Three ,frameset Just like a normal label , Does not affect element positioning
and frame And iframe Yes selenium In terms of positioning, it's the same ,selenium There is a set of methods for frame To operate
frame There are two ways of being : One is nested , One is not nested
Switch frame:
driver.switch_to.frame() According to the element id perhaps index Switch frame
driver.switch_to.default_content() Switch to the default frame
driver.switch_to.parent_frame() Switch to the parent frame
Handle non nested iframe:
driver.switch_to.frame(frame_id)
driver.switch_to.frame(frame_index) frame nothing id It is processed according to the index , Index from 0 Start ,driver.switch_to_frame(0)
Handle nested iframe:
For nested first in iframe Parent node , Then go to the child node , Then you can process and operate the objects in the child nodes
driver.switch_to.frame(" Parent node ")
driver.switch_to.frame(" Child node ")
边栏推荐
- Batch capitalization of MySQL table names
- 顺序栈1.0版本
- Is the waiting insurance record a waiting insurance evaluation? What is the relationship between the two?
- Splicing audio files with ffmpeg-4.3
- 微信小程序自定义tabBar
- Can the OPDS SQL component pass process parameters to the next component through context
- 等保备案是等保测评吗?两者是什么关系?
- 刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
- Adding subscribers to a list using mailchimp's API V3
- 云计算发展的 4 个阶段,终于有人讲明白了
猜你喜欢

顺序栈遍历二叉树

Visitor model -- generation gap between young and middle-aged people

Adding subscribers to a list using mailchimp's API V3

Implement the redis simple client customized based on socket

The latest simulated question bank and answers of the eight members (Electrical constructors) of Sichuan architecture in 2022

Several common command operations in win system

When querying the database with Gorm, reflect: reflect flag. mustBeAssignable using unaddressable value

Dongyuhui is not enough to bring goods to "rescue" live broadcast

网络安全审查办公室对知网启动网络安全审查

Builder mode -- Master asked me to refine pills
随机推荐
Undo log and redo log must be clear this time
JMeter parameterization
Apple, Microsoft and Google will no longer fight each other. They will work together to do a big thing this year
Second understanding permutation and combination
Prototype mode -- clone monster Army
Leetcode (135) - distribute candy
图的基本概念以及相关定义
海泰前沿技术|隐私计算技术在医疗数据保护中的应用
Sequence stack version 1.0
[performance tuning basics] performance tuning strategy
Can the OPDS SQL component pass process parameters to the next component through context
虚拟化是什么意思?包含哪些技术?与私有云有什么区别?
Open programmable infrastructure (OPI) project, redefining dpu/ipu
Interpreter mode -- formulas for dating
Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!
The Google File System (GFS) learning notes
"Super point" in "Meng Hua Lu", is the goose wronged?
Memo mode - game archiving
Pyaudio audio recording
The JS method parameter passed a number beginning with 0. A magical problem occurred and bothered me for a long time