当前位置:网站首页>Selenium advanced operations
Selenium advanced operations
2022-07-06 19:43:00 【Cold Lane (* _*)】
No interface mode
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get('https://www.baidu.com/')
perform js Code , Pull the page to the bottom
driver.execute_script(
'window.scrollTo(0, document.body.scrollHeight)'
)
Simulate keyboard keys
# Enter cold Lane in the search box
dirver.find_element(By.ID, 'kw').send_keys(' deserted alleyways ')
# Enter a space
dirver.find_element(By.ID, 'kw').send_keys(Keys.SPACE)
# ctrl+a Simulate select all
dirver.find_element(By.ID, 'kw').send_keys(Keys.CANCEL,'a')
# ctrl+c Simulate replication
dirver.find_element(By.ID, 'kw').send_keys(Keys.CANCEL,'c')
# ctrl+v Simulate paste
dirver.find_element(By.ID, 'kw').send_keys(Keys.CANCEL,'v')
# Simulated carriage return
dirver.find_element(By.ID, 'kw').send_keys(Keys.ENTER)
Simulation mouse
Import mouse event class
from selenium.webdriver import ActionChains
Instantiate mouse events
ActionChains(dirver)
Specify mouse behavior
move_to_element()
Execute mouse behavior
perform()
Here is an example
I want to click Advanced Search
Import
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium import webdriver
Open the browser
dirver = webdriver.Chrome()
dirver.get('https://www.baidu.com/')
Find the location of the setting
Advanced search location has also been found
# Move to settings
set_node = dirver.find_element(By.XPATH, '//*[@id="s-usersetting-top"]')
ActionChains(dirver).move_to_element(to_element=set_node).perform()
# Find the advanced search node , And click the
dirver.find_element(By.LINK_TEXT, ' Advanced search ').click()
Run it to see the effect
a !
Switch handle
Use URL type
Open the link on the page and a new window appears , But browser objects dirver Or the object of the previous page , You need to switch to a different window mirroring operation
Application plan dirver.switch_to.window()
Get all current handles first ( list )
Then switch to the specified handle ( utilize )
all_handles = dirver.window_handles
dirver.switch_to.window(all_handles[1]) # 1 Is the second tab
What is the handle ?
Simply put
I click Ice and snow as the medium An appointment to the Winter Olympics after , There is one more label in the label column above , This is the handle .
selenium frame
characteristic
Web pages are nested in web pages , Switch to frame, Then perform other operations
Processing steps
Switch to the... To be processed frame
stay frame Locate and operate page elements in
Returns the currently processed frame It is the upper level page or main page
Common methods
Switch to frame: driver.switch_to.frame(frame Node object )
Return to upper level :driver.switch_to.parent_frame()
Go back to the main page ;driver.switch_to.default_content()
Instructions
The default support id and name Attribute value lookup :switch_to.frame(id|name)
First find frame node :frame_node = driver.find_element_by_
Switch to frame:dirver.switch_to.frame(frame_node)
边栏推荐
猜你喜欢
[play with Linux] [docker] MySQL installation and configuration
ZABBIX proxy server and ZABBIX SNMP monitoring
谷粒商城--分布式高级篇P129~P339(完结)
A5000 vGPU显示模式切换
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
Dark horse -- redis
Mysql Information Schema 学习(二)--Innodb表
Interview assault 63: how to remove duplication in MySQL?
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
It's enough to read this article to analyze the principle in depth
随机推荐
Learn to explore - use pseudo elements to clear the high collapse caused by floating elements
反射及在运用过程中出现的IllegalAccessException异常
short i =1; I=i+1 and short i=1; Difference of i+=1
MySQL information schema learning (II) -- InnoDB table
DOM operation
[translation] Digital insider. Selection process of kubecon + cloudnativecon in Europe in 2022
[translation] linkerd's adoption rate in Europe and North America exceeded istio, with an increase of 118% in 2021.
Phoenix Architecture 3 - transaction processing
在解决了 2961 个用户反馈后,我做出了这样的改变...
Teach you to learn JS prototype and prototype chain hand in hand, a tutorial that monkeys can understand
【计算情与思】扫地僧、打字员、信息恐慌与奥本海默
Dark horse -- redis
It's super detailed in history. It's too late for you to read this information if you want to find a job
Leetcode 30. Concatenate substrings of all words
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
手把手教你学会js的原型与原型链,猴子都能看懂的教程
Alibaba数据源Druid可视化监控配置
POJ 3207 Ikki's Story IV – Panda's Trick (2-SAT)
Leetcode 30. 串联所有单词的子串
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go