Element positioning
#Coding=utf-8 fromSeleniumImportWebdriver fromSelenium.webdriver.firefox.firefox_binaryImportfirefoxbinaryImporttimebinary= Firefoxbinary ('/applications/firefoxdeveloperedition.app/contents/macos/firefox-bin') Driver= Webdriver. Firefox (firefox_binary=binary) Driver.get ('http://www.baidu.com') Size= driver.find_element_by_id ('kw'). SizePrint(size) text= driver.find_element_by_id ('CP'). TextPrint(text) size1= driver.find_element_by_id ('kw'). Get_attribute ('type')Print(size1) size2= driver.find_element_by_id ('kw'). is_displayed ()Print(size2) driver.find_element_by_id ('kw'). Clear () driver.find_element_by_id ('kw'). Send_keys ('Selenium') driver.find_element_by_id ('su'). Click () time.sleep (2) Driver.get ('http://news.baidu.com') Time.sleep (2) Driver.back () Time.sleep (2) Driver.forward () Time.sleep (2) Driver.refresh ()
Mouse keyboard operation
#Coding=utf-8 fromSeleniumImportWebdriver fromSelenium.webdriver.firefox.firefox_binaryImportfirefoxbinary fromSelenium.webdriver.common.action_chainsImportActionchains fromSelenium.webdriver.common.keysImportKeysImporttimebinary= Firefoxbinary ('/applications/firefoxdeveloperedition.app/contents/macos/firefox-bin') Driver= Webdriver. Firefox (firefox_binary=binary) Driver.get ('http://www.baidu.com') Right_click= driver.find_element_by_id ("Listholder")#Right-clickactionchains (Driver). Context_click (Right_click). Perform ()#Mouse Hoverabove = Driver.find_element_by_name ("Tj_settingicon") Actionchains (driver). Move_to_element (above). Perform ()#Double clickDouble_click = driver.find_element_by_id ('xx') Actionchains (driver). Double_click (Double_click). Perform ()#DragDrag_start = driver.find_element_by_id ('xx') Drag_end= driver.find_element_by_id ('cc') Actionchains (driver). Drag_and_drop (Drag_start,drag_end). Perform ()#Input Box input contentDRIVER.FIND_ELEMENT_BY_ID ('kw'). Send_keys ('HHA')#Delete multi-input contentDRIVER.FIND_ELEMENT_BY_ID ('kw'). Send_keys (Keys.back_space)#Enter a spaceDRIVER.FIND_ELEMENT_BY_ID ('kw'). Send_keys (Keys.space)#Ctrl + A Select allDRIVER.FIND_ELEMENT_BY_ID ('kw'). Send_keys (Keys.control,'a')#EnterDRIVER.FIND_ELEMENT_BY_ID ('kw'). Send_keys (Keys.enter)
Explicit/implicit wait
#Coding=utf-8 fromSeleniumImportWebdriver fromSelenium.webdriver.firefox.firefox_binaryImportfirefoxbinary fromSelenium.webdriver.common.byImport by fromSelenium.webdriver.support.uiImportwebdriverwait fromSelenium.webdriver.supportImportExpected_conditions as Ecbinary= Firefoxbinary ('/applications/firefoxdeveloperedition.app/contents/macos/firefox-bin') Driver= Webdriver. Firefox (firefox_binary=binary) Driver.get ('http://www.baidu.com')#Explicit Waitelement = Webdriverwait (driver,5,0.5). Until (Ec.presence_of_all_elements_located (by.id,'kw')) ) Element.send_keys ('SS')#Implicit wait fromSelenium.common.exceptionsImportnosuchelementexceptiondriver.implicitly_wait (10) Driver.get ('http://www.baidu.com')Try: driver.find_element_by_id ('kw'). Send_keys ('SS')exceptnosuchelementexception as E:Print(e)
Window/frame Switch
#Coding=utf-8 fromSeleniumImportWebdriver fromSelenium.webdriver.firefox.firefox_binaryImportfirefoxbinarybinary= Firefoxbinary ('/applications/firefoxdeveloperedition.app/contents/macos/firefox-bin') Driver= Webdriver. Firefox (firefox_binary=binary) Driver.get ('http://www.baidu.com')#iframe Toggle ID or name, otherwise use XPath to locateDriver.switch_to.frame ("if")#back to previous pageDriver.switch_to.parent_frame ()#Multi-Window switching#get the current window handleSearch_windows=Driver.current_window_handledriver.find_element_by_link_text ('Login'). Click () driver.find_element_by_link_text ('Register Now'). Click ()#gets all the currently open window handlesAll_handles =Driver.window_handles#go to registration window forHandleinchAll_handles:ifHandle! =Search_windows:driver.switch_to.window (handle)Print('Register ... ') #fill in the information Send_keys#back to the previous window forHandleinchAll_handles:ifHandle = =Search_windows:driver.switch_to.window (handle)Print('search ... ')
Selenium Webdriver API