python入门:悄悄的,来一个某东自动登录

  1. 唤醒浏览器打开主页。

  2. 切换到登录入口。

  3. 自动输入用户名和密码。

  4. 完成自动登录。

进群:700341555获取各类Python资料!

1)唤醒浏览器打开主页。

https://passport.jd.com/new/login.aspx?ReturnUrl=https%3A%2F%2Fwww.jd.com%2F

《python入门:悄悄的,来一个某东自动登录》 python入门:悄悄的,来一个某东自动登录

2)切换到登录入口。

因为默认进来是移动端的二维码登录入口,因此需要点击到【账户登录】这个入口。

(本来截图用二维码那个图,但是审核不通过,所以就用切换后的截图了)

《python入门:悄悄的,来一个某东自动登录》 python入门:悄悄的,来一个某东自动登录

进过分析,发现这个a链接的text是【账户登录】

因此通过下面的方法就可以定位。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>driver.find_element_by_link_text(“账户登录”).click()</pre>

《python入门:悄悄的,来一个某东自动登录》 python入门:悄悄的,来一个某东自动登录

3)自动输入用户名和密码。

通过同样的办法,看到用户名的id是loginname,密码的id是nloginpwd。这次我们通过id来定位。这里需要注意的是,大家需要自己替换自己的用户名和密码。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>driver.find_element_by_id(“loginname”).send_keys(“自己的用户名”)
driver.find_element_by_id(“nloginpwd”).send_keys(“自己的密码”)</pre>

《python入门:悄悄的,来一个某东自动登录》 python入门:悄悄的,来一个某东自动登录

4)完成自动登录。

最后需要做的是,定位登录按钮的元素。这里注意点是,登和录字之间是有空格的,需要复制粘贴一下。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>driver.find_element_by_link_text(“登 录”).click()</pre>

《python入门:悄悄的,来一个某东自动登录》 python入门:悄悄的,来一个某东自动登录

总结:废话不说,把完整的源代码贴上来啦。为了美观需要,这里用图片来展示啦。各位同学敲敲代码,边学边做,然后就能会有飞一般的感觉啦。

《python入门:悄悄的,来一个某东自动登录》 python入门:悄悄的,来一个某东自动登录

    原文作者:浪里小白龙q
    原文地址: https://www.jianshu.com/p/7242d0d28993
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞