selenium-free operation of cookies achieve dense login, automatic micro-Bo

Always wanted to achieve a small function with selenium, such as micro-Bo and the like, but some sites have a verification code to log in, I did not expect very good solutions, so think of using cookies to visit the website

The first step: Get a cookies available, get the cookies will be saved as a json file format. But after opening the website, log in manually operating needs its own look, there are comments in the code, we can note , as follows:

# ! / Usr / bin / Python 
# Coding = UTF-8 

from the Selenium Import webdriver
 Import Time
 Import json 

DEF get_cookies (test_url):
     # save the cookies file 
    File = ' cookies.json ' 
    # Open site requires cookies to obtain 
    Driver = webdriver .Firefox () 
    driver.implicitly_wait ( 5 ) 
    driver.get (test_url) 
    driver.maximize_window () 
    # after the site opened, executed manually log operation during the time 
    the time.sleep (60 )
     # after successful login, get cookies and saved as json format 
    cookies = driver.get_cookies()
    fp = open(file,'w')
    json.dump(cookies,fp)
    fp.close()
    #关闭浏览器
    driver.close()

if __name__ == "__main__":
    url = raw_input('please input url:')
    get_cookies(url)

Step two: use cookies visit the website, first open the site to be registered, and then read the cookies saved from the previous json file out, and then added to the browser, refresh the interface, you can log on to achieve, and then they can be made to it the operation, targeting specific elements, please Baidu some basic usage of selenium, not repeat them here. The code also gives the browser does not open the way to achieve the function.

code show as below:

# ! / Usr / bin / Python 
# Coding = UTF-8 

from the Selenium Import webdriver
 Import json
 Import Time 

File = ' cookies.json ' 
# open the browser, if do not want to open the browser to run scripts, you can use the following to comment out the 
driver = webdriver.Firefox () 

# in headless mode script, can be achieved without opening a browser on the Web site operated 
# the Option = webdriver.FirefoxOptions () 
# option.add_argument (argument = '- headless') 
# Driver = webdriver.Firefox ( the Option = firefox_options) 

# open URL 
DEF OPEN_URLrequest one (url): 
    driver.get (url) 
    driver.implicitly_wait (5 ) 
    driver.maximize_window () 

# add the saved cookies to the browser 
DEF add_cookies (): 
    fp = Open (File, ' r ' ) 
    cookies = the json.load (fp) 
    fp.close () 
    for the cookie in cookies: 
        Driver. add_cookie (the cookie)   

IF  __name__ == " __main__ " :
     # enter to open the Web site and open 
    test_url = raw_input ( ' Please the iNPUT url: ' ) 
    OPEN_URLrequest one (test_url) 
    the time.sleep ( 5 )
     #After adding cookies to refresh the interface, enables hands-free dense login 
    add_cookies () 
    driver.refresh () 
    # send microblogging 
    # locate the microblogging input box, click on the input box 
    driver.find_element_by_xpath ( " // the TextArea [@ class = 'W_input'] " ) .click () 
    the time.sleep ( 2 )
     # contents need to send input 
    driver.find_element_by_xpath ( " // TextArea [@ class = 'W_input'] " ) .send_keys ( ' the Test Message ' ) 
    the time.sleep ( . 3 )
     # click the send button 
    driver.find_element

 

Guess you like

Origin www.cnblogs.com/zhazi/p/11315818.html