Common methods for Actionchains-class mouse operations:
introduce the Actionchains class: From selenium.webdriver.common.action_chains import actionchains
Context_click () Right- click
--such as: RightClick = driver.find_element_by_id ("id")
Actionchains (Driver). Context_click (RightClick). Perform ()
Double_click () double-click
--such as: DoubleClick = Driver.find_element_by_name ("name")
Actionchains (Driver). Double_click (DoubleClick). Perform ()
Drag_and_drop (source, target) mouse drag and drop
--source: Mouse-pressed source element; target: Mouse-Released object element
--such as: element = Driver.find_element_by_name ("name")
target = Driver.find_element_by_name ("name")
Actionchains (Driver). Drag_and_drop (element, target). Perform ()
move_to_element () hover over an element
--such as: above = Driver.find_element_by_xpath ("XPath path")
Actionchains (Driver). Move_to_element (above). Perform ()
click_and_hold () Press the left mouse button on an element
--such as: left = Driver.find_element_by_name ("name")
Actionchains (Driver). Click_and_hold (left). Perform ()
Keyboard events:
To introduce the keys class package: From Selenium.webdriver.common.keys import keys
Send_keys () input Box input content
-such as: driver.find_element_by_id ("id"). Send_keys ("XXX")
Send_keys (keys.back_space) deletes one character to the left-such as: driver.find_element_by_id ("id"). Send_keys ("XXX")
Send_keys (keys.space) Enter a space
-such as: driver.find_element_by_id ("id"). Send_keys (Keys.space)
Send_keys (Keys.control, ' a ') Ctrl + A Select all the contents of the input box
-such as: driver.find_element_by_id ("id"). Send_keys (Keys.control, ' a ')
Send_keys (Keys.control, ' x ') Ctrl+x cut the contents of the input box
-such as: driver.find_element_by_id ("id"). Send_keys (Keys.control, ' x ')
Send_keys (Keys.control, ' V ') Ctrl + V paste into the input box
-such as: driver.find_element_by_id ("id"). Send_keys (Keys.control, ' V ')
Send_keys (keys.enter) enter instead of click
-such as: driver.find_element_by_id ("id"). Send_keys (Keys.enter)
Send_keys (keys.tab) Tab key (tab)
-such as: driver.find_element_by_id ("id"). Send_keys (Keys.tab)
Send_keys (keys.escape) fallback key (ESC)
-such as: driver.find_element_by_id ("id"). Send_keys (Keys.escape)
Send_keys (Keys.control, ' C ') copy
-such as: driver.find_element_by_id ("id"). Send_keys (Keys.control, ' C ')
Printing information
#获得title并打印
title = Driver.title
Print title
#拿当前title名称进行预期比较
if title = = U "Baidu a bit, you will know":
Print "Title yes!"
Else
Print "Title no!"
#获得当前URL并打印
URL = Driver.current_url
Print URL
Wait time
#导入 webdriverwait Bag
From Selenium.webdriver.support.ui import webdriverwait
#导入 Time Package
Import time
sleep () set a fixed wait time
--such as: Time.sleep (5) #等待5秒
implicitly_wait () waits for an element to be found, or a command completes, and throws an exception if it exceeds the set time
--such as: driver.implicitly_wait (30)
DRIVER.FIND_ELEMENT_BY_ID ("id"). Click ()
webdriverwait () in the set time, the default is to detect the presence of the current page element at intervals of time, and throw an exception if it is not detected more than the set time
Webdriverwait (Driver, timeout, poll_frequency=0.5, Ignored_exceptions=none)
--driver:webdriver drivers (Ie, Firefox, Chrome or remote)
--timeout: Maximum time-out, default in seconds
--poll_frequency: Interval (step) time for sleep time, default is 0.5 seconds
--ignored_exceptions: Exception information after timeout, nosuchelementexception exception thrown by default
--such as 1:element = Webdriverwait (Driver). Until (Lambda x:x.find_element_by_id ("id"))
Element.send_keys ("Selenium")
--such as 2:element = Webdriverwait (Driver). Until (lambda x:x.find_element_by_id ("id"))
is_disappeared = webdriverwait (Driver, 1, (Elementnotvisibleexception)). Until_not (Lambda x:x.find_element_by _id ("Someid"). is_displayed ())
Webdriverwai () is generally used in conjunction with the unit () or Until_not () method:
--until (method, message= ")
Call the method to provide the driver as a parameter until the return value is not False.
--until_not (method, message= ")
Call the method to provide the driver as a parameter until the return value is False.
Locating a group of objects
#选择当前页面上所有tag the element name is input
Inputs = driver.find_elements_by_tag_name (' input ')
#从中过滤出type为checkbox的元素, and Tick on
For input in inputs:
If Input.get_attribute (' type ') = = ' checkbox ':
Input.click ()
#使用CSS定位选择所有type为checkbox的元素, and Tick on
checkboxes = Driver.find_elements_by_css_selector (' input[type=checkbox] ')
For checkbox in checkboxes:
Checkbox.click ()
#把最后一个checkbox的勾去掉 pop () is empty is the last
Driver.find_elements_by_css_selector (' Input[type=checkbox] '). Pop (). Click ()
Python selenium--common functions 3