selenium + Python (mouse and keyboard events)

       Benpian summarizes the web pages of some common operating element method, referred to as behavioral event
some web interface options menu hover need to show up (such as the Settings button Baidu pages) on an element.

1 simple operation

1. Click (left mouse button) Page button: the Click ()
2. Please enter the empty box: the Clear ()
3. Enter the string: send_keys ()
4. Open the test tribal forum, click the magnifying glass search icon, usually in order to ensure input correctness, you can clear
space under the entry box, then enter your search keywords

?
1
2
3
4
5
6
7
8
# coding:utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get( "http://www.baidu.com " )
driver.implicitly_wait( 10 )
driver.find_element_by_id( "kw" ).click()
driver.find_element_by_id( "kw" ).clear()
driver.find_element_by_id( "kw" ).send_keys( "selnium" )

2.submit submit the form

1. In the case in front of the Baidu search, enter keywords, press the Enter key to search directly, you can also point the search button to search.
2.submit () is generally used to simulate the Enter key

?
1
2
3
4
5
6
7
8
# coding:utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get( "http://www.baidu.com " )
driver.implicitly_wait( 10 )
driver.find_element_by_id( "kw" ).send_keys( "selnium" )
# submit() 模拟enter键提交表单
driver.find_element_by_id( "kw" ).submit()

3 keyboard

1.selenium provides a set of simulated keyboard event, the front submit () method, if feasible, can be
to try to simulate keyboard events
2. Analog operation of the keyboard the keyboard module needs to be imported: from selenium.webdriver.common.keys
Import keys
3. analog enter key, can send_keys (Keys.ENTER)

?
1
2
3
4
5
6
7
8
9
10
11
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get( "http://www.baidu.com " )
driver.implicitly_wait( 10 )
driver.find_element_by_id( "kw" ).click()
driver.find_element_by_id( "kw" ).clear()
driver.find_element_by_id( "kw" ).send_keys( "selnium" )
# 模拟enter键操作回车按钮
driver.find_element_by_id( "kw" ).send_keys(Keys.ENTER)

4. Other common keyboard:
Keyboard F1 to F12: send_keys (Keys.F1) into the corresponding shortcut key F1
copy Ctrl + C: send_keys (Keys.CONTROL, 'c')
Paste Ctrl + V: send_keys (Keys .CONTROL, 'v')
Select all Ctrl + A: send_keys (Keys.CONTROL, 'a')
cut Ctrl + X: send_keys (Keys.CONTROL, 'x')
tab key tab: send_keys (Keys.TAB)
here we are just listed some common, of course, in addition to keyboard events, mouse events there

4 mouseover event

1. Not only can mouse click (click), Mouse there are other operations, such as: hover over an element, right-click the mouse, hold down the mouse button and drag a
2 mouse event need to import the module: selenium.webdriver.common.action_chainsimport ActionChains from
the perform () performs all ActionChains behavior in
move_to_element () hover
3. page Setup button here to Baidu for example
4. in addition to the usual mouse-over events, and
right-click : context_click ()
Double-click the mouse: double_click ()

?
1
2
3
4
5
6
7
8
9
10
11
# coding:utf-8
from selenium import webdriver
# 引入ActionChains类
from selenium.webdriver.common.action_chains import ActionChains
# 使用方法
brower = webdriver.Firefox()
brower.get( "http://www.baidu.com" )
# 定位到需要右击的元素并赋值给rigth_click
right_click = brower.find_element_by_id( "kw" )
# 对定位到的元素进行右击操作。
ActionChains(brower).context_click(right_click).perform()

Transfer: https://www.cnblogs.com/101718qiong/category/1059473.html

       Benpian summarizes the web pages of some common operating element method, referred to as behavioral event
some web interface options menu hover need to show up (such as the Settings button Baidu pages) on an element.

1 simple operation

1. Click (left mouse button) Page button: the Click ()
2. Please enter the empty box: the Clear ()
3. Enter the string: send_keys ()
4. Open the test tribal forum, click the magnifying glass search icon, usually in order to ensure input correctness, you can clear
space under the entry box, then enter your search keywords

?
1
2
3
4
5
6
7
8
# coding:utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get( "http://www.baidu.com " )
driver.implicitly_wait( 10 )
driver.find_element_by_id( "kw" ).click()
driver.find_element_by_id( "kw" ).clear()
driver.find_element_by_id( "kw" ).send_keys( "selnium" )

2.submit submit the form

1. In the case in front of the Baidu search, enter keywords, press the Enter key to search directly, you can also point the search button to search.
2.submit () is generally used to simulate the Enter key

?
1
2
3
4
5
6
7
8
# coding:utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get( "http://www.baidu.com " )
driver.implicitly_wait( 10 )
driver.find_element_by_id( "kw" ).send_keys( "selnium" )
# submit() 模拟enter键提交表单
driver.find_element_by_id( "kw" ).submit()

3 keyboard

1.selenium 提供了一整套的模拟键盘操作事件,前面 submit()方法如果可行的话,可
以试试模拟键盘事件
2.模拟键盘的操作需要先导入键盘模块:from selenium.webdriver.common.keys
import Keys
3.模拟 enter 键,可以用 send_keys(Keys.ENTER)

?
1
2
3
4
5
6
7
8
9
10
11
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get( "http://www.baidu.com " )
driver.implicitly_wait( 10 )
driver.find_element_by_id( "kw" ).click()
driver.find_element_by_id( "kw" ).clear()
driver.find_element_by_id( "kw" ).send_keys( "selnium" )
# 模拟enter键操作回车按钮
driver.find_element_by_id( "kw" ).send_keys(Keys.ENTER)

4.其它常见的键盘操作:
键盘 F1 到 F12:send_keys(Keys.F1) 把 F1 改成对应的快捷键
复制 Ctrl+C:send_keys(Keys.CONTROL,'c')
粘贴 Ctrl+V:send_keys(Keys.CONTROL,'v')
全选 Ctrl+A:send_keys(Keys.CONTROL,'a')
剪切 Ctrl+X:send_keys(Keys.CONTROL,'x')
制表键 Tab: send_keys(Keys.TAB)
这里只是列了一些常用的,当然除了键盘事件,也有鼠标事件

4 鼠标悬停事件

1.鼠标不仅仅可以点击(click),鼠标还有其它的操作,如:鼠标悬停在某个元素上,鼠标右击,鼠标按住某个按钮拖到
2.鼠标事件需要先导入模块:from selenium.webdriver.common.action_chainsimport ActionChains
perform() 执行所有 ActionChains 中的行为
move_to_element() 鼠标悬停
3.这里以百度页面设置按钮为例
4.除了常用的鼠标悬停事件外,还有
右击鼠标:context_click()
双击鼠标:double_click()

?
1
2
3
4
5
6
7
8
9
10
11
# coding:utf-8
from selenium import webdriver
# 引入ActionChains类
from selenium.webdriver.common.action_chains import ActionChains
# 使用方法
brower = webdriver.Firefox()
brower.get( "http://www.baidu.com" )
# 定位到需要右击的元素并赋值给rigth_click
right_click = brower.find_element_by_id( "kw" )
# 对定位到的元素进行右击操作。
ActionChains(brower).context_click(right_click).perform()

Guess you like

Origin www.cnblogs.com/jiangmingbai/p/10969430.html