当前位置:网站首页>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 ")
边栏推荐
- Apple doesn't need money, but it has no confidence in its content
- Read all text from stdin to a string
- Mr. Hu Bo, CIO of weiduomei, a scientific innovator: digitalization is a bloodless revolution, and the correct answer lies in the field of business
- Leetcode (146) - LRU cache
- Rename and delete files
- 虚拟化是什么意思?包含哪些技术?与私有云有什么区别?
- The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars
- Common member methods of the calendar class
- C language to realize mine sweeping (simple version)
- 红象云腾完成与龙蜥操作系统兼容适配,产品运行稳定
猜你喜欢

"Super point" in "Meng Hua Lu", is the goose wronged?

顺序表的基本操作

二叉树的基本性质与遍历

Set up your own website (14)

Berkeley, MIT, Cambridge, deepmind et d'autres grandes conférences en ligne: vers une IA sûre, fiable et contrôlable

在Dialog中使用透明的【X】叉叉按钮图片

物聯網?快來看 Arduino 上雲啦

得物多活架构设计之路由服务设计

云计算发展的 4 个阶段,终于有人讲明白了

Basic properties and ergodicity of binary tree
随机推荐
NPM download speed is slow
Material management system based on SSM (source code + document + database)
Rename and delete files
虚拟化是什么意思?包含哪些技术?与私有云有什么区别?
Nifi fast authentication configuration
Adding subscribers to a list using mailchimp's API V3
Intermediary model -- collaboration among departments
Map跟object 的区别
物聯網?快來看 Arduino 上雲啦
Procedural life: a few things you should know when entering the workplace
Sequential stack traversal binary tree
在Dialog中使用透明的【X】叉叉按钮图片
Can the OPDS SQL component pass process parameters to the next component through context
Docker deploy mysql5.7
Bean lifecycle flowchart
Record a deletion bash_ Profile file
刚购买了一个MYSQL数据库,提示已有实例,控制台登录实例要提供数据库账号,我如何知道数据库账号。
Prototype mode -- clone monster Army
List set Introduction & common methods
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!