Selenium Webdriver API

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.