Selenium login website Get cookies request other data

Source: Internet
Author: User

1.selenium Login Website

1) Selenium access to the start URL must wait for the login box full rendering appears, no page browser can print out Page_source out to see if the login box has appeared

2) The Login box has occurred, if the User name input box input value, found an error, said to find this node, then see if in the IFRAME, in the words, need switch_to.frame into

3) The user name and password input interval should be similar to the time of human input, too fast may be found to be analog click to login

4) Some websites in the login button to set the barrier, you may find non-human operation, you can use the Enter operation instead, skip click Login This step, perfect to avoid

5) After login (to determine whether it is logged in, you can print the current current_url, see if it is the URL after landing) to obtain cookies (note that if you log in the IFRAME, you need to return to the main page) assignment to requests

6) Then use requests to request the website login page, you can respond normally, will not jump to the login page

Sites = []
s = requests. Session ()
Start_url = ' Https://passport.umeng.com/login?appId=cnzz '
Try
print ' Login Friends '
Driver.delete_all_cookies ()
Driver.get (Start_url)
# print Driver.current_url, Driver.page_source
Driver.switch_to.frame ("Alibaba-login-box") #进入登录iframe
Time.sleep (10)
Elem_user = driver.find_element_by_id ("Fm-login-id")
Time.sleep (10)
Elem_user.send_keys ("[email protected]");
Elem_pwd = driver.find_element_by_id (' Fm-login-password ')
Time.sleep (10)
Elem_pwd.send_keys ("tangdaoya2016")
Time.sleep (10)
Elem_pwd.send_keys (Keys.enter) #网站模拟点击登录按钮会出错, directly after the password is lost ENTER to log in
Time.sleep (10)
If Driver.current_url! = Start_url:
print ' Landing success ', Driver.current_url
Driver.switch_to_default_content () #需要返回主页面, otherwise the cookies obtained are not cookies after landing
List_cookies = Driver.get_cookies ()
cookies = {}
For S in List_cookies:
cookies[s[' name ']] = s[' value ']
Requests.utils.add_dict_to_cookiejar (s.cookies, cookies) #将获取的cookies带入请求中
print ' Cookies: ', cookies,self.config.url_list
Url_list = ' ttps://web.umeng.com/main.php?c=site&a=show&ajax=module=list|module=isopentongji&search= &currentpage=1&pagetype=30&sort=0 ' #url_list是登陆后才能请求到数据的api
R = Json.loads (S.get (url_list). Content)
For temp in r[' data ' [' list '] [' Items ']:
If Isinstance (temp,dict):
Sites.append ({"SiteID": temp["SiteID"], "name": temp["Name"})

Except
print ' Login failed '
Traceback.print_exc ()
Finally
Driver.quit ()
Display.stop ()

Selenium login website Get cookies request other data

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.