Login process:
- Examples of a driver, then driver.get () sends a request
- The most important: Switch iframe sub-frame, because that part of the login page watercress is a ifrme, you must switch to find the corresponding element
- The use of selenium to switch to the login account password
- The use of selenium enter the account and password
- The use of selenium click the login button
- Then use the dictionary comprehension save a little cookie
Code:
. 1 Import Time 2 from Selenium Import the webdriver . 3 . 4 # Example of Driver . 5 Driver = webdriver.Chrome () . 6 driver.get ( ' https://www.douban.com/ ' ) . 7 # # iframe switching sub-frame . 8 Driver. switch_to.frame (driver.find_elements_by_tag_name ( " iframes " ) [0]) . 9 10 driver.maximize_window () # maximize the window . 11 driver.find_element_by_css_selector ( ' li.account-Account-Tab ') .click () # Click password label 12 driver.find_element_by_id ( ' username ' ) .send_keys ( ' 13,343,396,443 ' ) 13 driver.find_element_by_id ( ' password ' ) .send_keys ( ' lilei120400 ' ) 14 # Click 'Login bean' button 15 # It should be noted that when the class attribute has several elements, the parameters of this function to fill a first class like 16 driver.find_element_by_class_name ( ' btn ' ) .click () # class attribute of an element: btn Account-BTN . 17 # obtain cookies, Dictionary derivation 18 cookies = {i['name']: i['value'] for i in driver.get_cookies()} 19 print(cookies) 20 21 time.sleep(5) 22 driver.quit() # 退出浏览器