A. Dialog box:
Example: Click Baidu Login, pop up the small window
#coding =utf-8from Selenium Import webdriverfrom time import sleepdr=webdriver. Chrome () dr.get ("http://www.baidu.com") #一定要记得设置等待时间, otherwise not located!!! Dr.find_element_by_link_text ("Login"). Click () Sleep (3)#思路: two times to locate, click Login, locate the login pop-up box, with ID or class_name can!! Then locate the login box's name input box #login=dr.find_element_by_id ("Tangram__psp_8__username") to navigate directly, not to the!!!!! #login =dr.find_element_by_class_name ("Tang-content"). Find_element_by_name ("UserName") login=dr.find_ element_by_id ("Tangram__psp_8__form"). find_element_by_id ("Tangram__psp_8__username") Login.send_keys (" 17710192039 ") dr.find_element_by_name (" password "). Send_keys (" a7s5dfg! ") dr.find_element_by_id ("Tangram__psp_8__submit"). Submit ()
Two. Multi-window, jump from one page to another page
Idea: First locate Baidu login handle, then jump to the registration page, click to go to the non-login page handle!!!!!
#coding =utf-8from Selenium Import webdriverfrom time import sleepdr=webdriver. Chrome () dr.get ("http://passport.baidu.com")#一定要记得设置等待时间, otherwise not located!!! nowhandle=dr.current_window_handleallhandles0=dr.window_handles#当前登录页面只有一个句柄print "Now is the login page" Print nowhandlefor h in allhandles0: Print h#跳转到注册页面一共有两个句柄 (including login page)print "Go to registration page now" Dr.find_ Element_by_link_text ("Register Now"). Click () Sleep (3) Allhandles=dr.window_handlesfor handle in Allhandles: print Handlefor handle in Allhandles: if Handle!=nowhandle: Dr.switch_to_window (handle)sleep (3) # Close the current Window Dr.close ()#转到登录首页dr. Switch_to_window (Nowhandle)
three. Popup handling alert: Using Switch_to_alert () (Accept,dismiss,send_keys)
#coding = Utf-8from Selenium Import webdriverfrom selenium.webdriver.common.action_chains import actionchainsfrom time Import Sleepdr=webdriver. Chrome () dr.get ("http://www.baidu.com") #此处为百度页面的设置, involving the processing of drop-down boxes!!! Processing ideas: Generally two clicks, one click Pop-up drop-down box, another click on the option, if the mouse is moved on the pop-up with move_to_element () #先定位到 "Settings" Dr.find_element_by_link_text ("Settings "). Click () Sleep (3) sou=dr.find_element_by_class_name (" PF ") Sou.click () #ActionChains (DR). Move_to_element ( SOU). Perform () This is generally used to find the parent element, drop-down box. Child element, move to child element on sleep (3) #点击 OK, with accept () Dr.switch_to_alert (). Accept () #点击取消, with dismiss () Dr.switch_to_alert (). Dismiss () #输入内容, directly with Send_keys () Dr.switch_to_alert (). Send_keys () #输出内容 print Dr.switch_to_alert (). Text
Four. Upload the file, directly locate the click button, and Send_keys (the path must be correct)
Upload.html
uoload.py
#coding =utf-8from Selenium Import webdriverfrom time import Sleepimport osdr=webdriver. Chrome () #打开上传文件的页面file_path = ' file:///' +os.path.abspath (' upload.html ') dr.get (File_path) sleep (3) #点击 The Select File button, Dr.find_element_by_name ("file"). Send_keys (' D:\\zhihu.cookie.txt ') sleep (3)
The effect is as follows:
Python instance writing (3)--dialog box, multi-window, drop-down box, upload file