Python3 use Selenium library to login and save cookies as local files

Source: Internet
Author: User
Tags xpath

Python3 use Selenium library to login and save cookies as local files

Learn to use Selenium library to simulate landing, and save the cookie as a local file, then for later use (requests module), with the selenium simulation Landing, More smoothly, did not encounter the need to verify code, the code is placed in a module named :

1 #-*-coding:utf-8-*-2 3  fromSelenium.webdriverImportChrome4  fromSelenium.webdriver.supportImportExpected_conditions as EC5  fromSelenium.webdriver.common.byImport by6  fromSelenium.common.exceptionsImporttimeoutexception8  fromSettingsImport*9 Import TimeTen ImportOS One ImportJSON A  -  - defZhihu_login (): the     #Login URL -Login_url ='Https://' -Brower =Chrome () -     #set browser window to maximize + Brower.maximize_window () -     #Response Wait Time +wait = Webdriverwait (brower, 5) A brower.get (Login_url) at     Try: -         #Simulation Click on the bottom of the landing link book, jump to the account password login interface -Login_switch = Wait.until (ec.element_to_be_clickable (By.xpath,'//*[@id = "root"]/div/main/div/div/div/div[2]/div[2]/span'))) - () -         #analog Input User name -Username_input = Wait.until (ec.presence_of_element_located (By.xpath,'//*[@id = "root"]/div/main/div/div/div/div[2]/div[1]/form/div[1]/div[2]/div[1]/input'))) in Username_input.send_keys (username) -         #Analog Input Password toPassword_input = Wait.until (ec.presence_of_element_located (By.xpath,'//*[@id = "root"]/div/main/div/div/div/div[2]/div[1]/form/div[2]/div/div[1]/input'))) + password_input.send_keys (password) -         #Analog Click the login button theLogin_button = Wait.until (ec.element_to_be_clickable (By.xpath,'//*[@id = "root"]/div/main/div/div/div/div[2]/div[1]/form/button'))) * () $Time.sleep (5)Panax Notoginseng         #Access to Cookies -cookies =brower.get_cookies () the         #Save Cookies as local files + savecookies (Cookies) A     excepttimeoutexception: the         #If you catch a time-out exception, you recursively call yourself + Zhihu_login () -  $  $ defsavecookies (cookies): -Path = OS.GETCWD () +'/cookies/' -     if  notos.path.exists (path): the os.makedirs (path) -with open (path +'Cookies.txt','W') as F:WuyiCookie =json.dumps (Cookies) the f.write (Cookie) -  Wu  - if __name__=='__main__': AboutZhihu_login ()

Due to personal habits, a configuration module was used:

1 # User name 2 ' 173866016XX ' 3 4 # Password 5 ' XXX '

After execution, a folder named Cookies is generated in the current working directory and thecookies.txt file is in that folder. In the event that the cookie is not expired, you will be able to access it later without having to log in again and load the cookie file directly.

The Chrome browser used above has an interface mode, if you do not want the browser to run directly in the background, do not pop up the interface, you can:

1  from Import Options 2 3 options = options ()4 options.add_argument ('--headless' )5 brower = Chrome (options=options)

For more detailed wording, refer to:

Python3 use Selenium library to login and save cookies as local files

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: 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.