Personal notes, excerpt from the insect Master Python selenum, for personal reference only
1. Installation:
Pip Install Selenium
Download Webdriver
# Webdriver downloaded and placed in the Python home directory 1.chromedriver:https://code.google.com/p/chromedriver/downloads/List2. The driver of Firefox driver geckodriver:https://github.com/mozilla/geckodriver/releases/3.ie iedriver:http://www.nuget.org/ packages/selenium.webdriver.iedriver/
2. Basic use
from Import == webdriver. Chrome () browser.set_window_size(480, +) # parameter number is pixel point browser.maximize_window () # maximize the browser display #
3. Element positioning
#single Elementbrowser.find_element_by_id () browser.find_element_by_name () browser.find_element_by_tag_name () Browser.find_ Element_by_class_name () Browser.find_element_by_css_selector () Browser.find_element_by_xpath ( )#multiple elementsbrowser.find_elements_by_[...] ()#level positioning#Click the Link1 link (pop-up drop-down list)Dr.find_element_by_link_text ('Link1'). Click ()#find the parent element with ID dropdown1Webdriverwait (DR, until).LambdaTHE_DRIVER:THE_DRIVER.FIND_ELEMENT_BY_ID ('DropDown1'). is_displayed ())#Locate the child element of link as action under the Father componentmenu = dr.find_element_by_id ('DropDown1'). Find_element_by_link_text ('Action')#Mouse navigates to child elementsWebdriver. Actionchains (DR). Move_to_element (menu). Perform ()#move the mouse over the hover elementActionchains (browser). Move_to_element (Ele). Perform ()
4, the operation of objects, webelement common methods, summary
Action object:
- · Click on Object
- · Send_keys analog key input on the object
- · Clear clears the contents of the object, if possible
Webelement Other common methods:
- · Text gets the literal of the element
- · Submit Form
- · Get_attribute Getting property values
5. Positioning frame
Multi-layered frame or window positioning:
- Switch_to_frame ()
- Switch_to_window ()
Smart wait:
6, using selenium Webdriver call JS
Invoking the JS method
- Execute_script (script, *args)
Synchronously executes JavaScript in the current window/frame
Script: Execution of JavaScript.
* Parameters: Applies to any JavaScript script.
Use:
Driver.execute_script (' Document.title ')
7, selenium the location of the drop-down box information, as well as the handling of the warning prompt box
This section focuses on
- Handling drop-down boxes
- Switch_to_alert ()
- Accept ()
8, through the JS control scroll bar to the bottom of the window
js="var q=document.getelementbyid (' id '). scrolltop=10000"driver.execute_script (JS ) JS="var q=document.documentelement.scrolltop=10000"driver.execute_script ( JS)
9, Analog keyboard key operation
- L Keyboard Key Usage
- L Keyboard Combination Key Usage
- L Send_keys () input Chinese operation error problem
fromSelenium.webdriver.common.keysImportKeys#need to introduce the keys packagedriver.find_element_by_id ("user_name"). Send_keys (Keys.tab) driver.find_element_by_id ("user_pwd"). Send_keys (Keys.enter) driver.find_element_by_id ("kw"). Send_keys (Keys.control,'a') driver.find_element_by_id ("kw"). Send_keys (Keys.control,'C') driver.find_element_by_id ("kw"). Send_keys (Keys.control,'v') Send_keys (U"Enter Chinese")
Module:selenium.webdriver.common.keys?classKeys () –null=u ' ue000 ' –cancel= U ' ue001 '#^breakHelp =u ' ue002 ' –back_space=u ' ue003 ' –tab=u ' ue004 ' –clear=u ' ue005 ' –return=u ' ue006 ' –enter=u ' ue007 ' –shift=u ' ue008 ' –left_shift= U ' ue008 '#alias–control =u ' ue009 ' –left_control= U ' ue009 '#alias–alt =u ' ue00a ' –left_alt= U ' ue00a '#alias–pause =u ' ue00b ' –escape=u ' ue00c ' –space=u ' ue00d ' –page_up=u ' ue00e ' –page_down=u ' ue00f ' –end=u ' ue010 ' –home=u ' ue011 ' –left=u ' ue012 ' –arrow_left= U ' ue012 '#alias–up =u ' ue013 ' –arrow_up= U ' ue013 '#alias–right =u ' ue014 ' –arrow_right= U ' ue014 '#alias–down =u ' ue015 ' –arrow_down= U ' ue015 '#alias–insert =u ' ue016 ' –delete=u ' ue017 ' –semicolon=u ' ue018 ' –equals=u ' ue019 ' –numpad0= U ' ue01a '#Numbe pad Keys–numpad1 =u ' ue01b ' –numpad2=u ' ue01c ' –numpad3=u ' ue01d ' –numpad4=u ' ue01e ' –numpad5=u ' ue01f ' –numpad6=u ' ue020 ' –numpad7=u ' ue021 ' –numpad8=u ' ue022 ' –numpad9=u ' ue023 ' –multiply=u ' ue024 ' –add=u ' ue025 ' –separator=u ' ue026 ' –subtract=u ' ue027 ' –decimal=u ' ue028 ' –divide=u ' ue029 ' –f1= U ' ue031 '#function keys–F2 =u ' ue032 ' –f3=u ' ue033 ' –f4=u ' ue034 ' –f5=u ' ue035 ' –f6=u ' ue036 ' –f7=u ' ue037 ' –f8=u ' ue038 ' –f9=u ' ue039 ' –f10=u ' ue03a ' –f11=u ' ue03b ' –f12=u ' ue03c ' –meta=u ' ue03d ' –command= U ' ue03d '
Keyboard Simulation
10. Operation of Cookies via Webdirver
- Driver.get_cookies () Get cookie information
- Add_cookie (COOKIE_DICT) Adding session information to cookies
- Delete_cookie (name) delete a specific (partial) cookie
- Delete_all_cookies () Delete all Cookies
11, Selenium Webdriver Mouse event usage
Actionchains class
- Context_click () Right-click
- Double_click () Double-click
- Drag_and_drop () drag
fromSelenium.webdriver.common.action_chainsImportActionchains#navigate to the element you want to double-clickQQQ =driver.find_element_by_xpath ("XXX")#perform a mouse double-click on the anchored elementactionchains (Driver). Double_click (QQQ). Perform ()#Position The original location of the elementelement = Driver.find_element_by_name ("Source")#position the target where the element is to be movedtarget = Driver.find_element_by_name ("Target")#performing a move operation on an elementActionchains (Driver). Drag_and_drop (element, target). Perform ()
[Python selenium] how to organize