当前位置:网站首页>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 ")
边栏推荐
- CVPR 2022 remembers Sun Jian! Tongji and Ali won the best student thesis award, and hekaiming was shortlisted
- Camera rental management system based on qt+mysql
- Analysis of errors in JSON conversion using objectmapper
- Selenium crawl notes
- Where is 5g really powerful? What is the difference with 4G?
- C语言实现扫雷(简易版)
- Basic properties and ergodicity of binary tree
- Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
- Simpledateformat thread unsafe
- The Google File System (GFS) learning notes
猜你喜欢

Combination mode -- stock speculation has been cut into leeks? Come and try this investment strategy!

How to apply agile development ideas to other work

大一女生废话编程爆火!懂不懂编程的看完都拴Q了
浅谈MySql update会锁定哪些范围的数据

物联网?快来看 Arduino 上云啦

Learn to use a new technology quickly

基于SSM的物料管理系统(源码+文档+数据库)

What does virtualization mean? What technologies are included? What is the difference with private cloud?

Variable setting in postman

顺序栈遍历二叉树
随机推荐
Common data model (updating)
Postman assertion
Implement the redis simple client customized based on socket
What does virtualization mean? What technologies are included? What is the difference with private cloud?
Nifi quick installation (stand-alone / cluster)
After 5 months' test, it took 15K to come for an interview. When I asked, it was not worth even 5K. It was really
List set Introduction & common methods
Stackoverflow annual report 2022: what are developers' favorite databases?
Sequence stack version 1.0
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
基于QT+MySQL的相机租赁管理系统
Where is 5g really powerful? What is the difference with 4G?
Internet of things? Come and see Arduino on the cloud
红象云腾完成与龙蜥操作系统兼容适配,产品运行稳定
Interpreter mode -- formulas for dating
Prototype mode -- clone monster Army
Design of routing service for multi Activity Architecture Design
Vant component used in wechat applet
Byte and Tencent have also come to an end. How fragrant is this business of "making 30million yuan a month"?
Microsoft Certification (dynamic 365) test