import json
from time import sleep
from selenium import webdriver
import chardet
from selenium.webdriver import ActionChains #初始化浏览器
# driver = webdriver.Chrome(executable_path = "e:\\chromedriver")
driver = webdriver.Firefox(executable_path = "e:\\geckodriver")
# driver = webdriver.Ie(executable_path = "e:\\IEDriverServer") #定义全局遍变量url
url = "https://www.jd.com" def login_cookie():
#打开浏览器
driver.get(url)
# 浏览器最大化
driver.maximize_window()
#定位登录button
driver.find_element_by_xpath('//a[@class = "link-login"]').click()
#定位账户登录
driver.find_element_by_xpath('//a[text()="账户登录"]').click()
#定位账号框,并输入账号
driver.find_element_by_xpath('//input[@name="loginname"]').send_keys("")
#定位密码框,并输入密码
driver.find_element_by_xpath('//input[@type="password"]').send_keys("GaoXXX")
#点击登录button
driver.find_element_by_xpath('//a[@id="loginsubmit"]').click()
sleep(10)
#需要手动滑动图片,通过校验 #获取cookie
my_cookie = driver.get_cookies()
print(my_cookie)
data_cookie = json.dumps(my_cookie)
with open("jd_coolies","w") as fp:
fp.write(data_cookie) #使用cookies
def get_url_with_cookies():
# 访问网站,清空旧cookies信息
driver.get(url)
driver.delete_all_cookies()
#获取cookies文件
with open("jd_coolies","r") as fp:
jd_cookies = fp.read()
#加载cookies信息
jd_cookies_dict = json.loads(jd_cookies)
for cookie in jd_cookies_dict:
driver.add_cookie(cookie)
#验证是否登录成功
driver.get(url)
assert '退出' in driver.page_source
print(url) # 添加购物车
def shopping():
driver.get('https://www.jd.com/')
# 定位搜索框,并输入:Python自动化
driver.find_element_by_xpath("//input[@clstag='h|keycount|head|search_c']").send_keys('Python自动化')
# 定位“搜索”button,并点击
driver.find_element_by_xpath('//button[@clstag="h|keycount|head|search_a"]/i').click()
# 获取当前窗口句柄
now_handle = driver.current_window_handle
# 打印当前窗口句柄
print("添加购物车窗口")
print(now_handle) #判断 不是 当前窗口句柄
# 获取所有窗口句柄
all_handles = driver.window_handles
# 循环遍历所有新打开的窗口句柄,也就是说不包括主窗口
for handle in all_handles:
if handle != now_handle:
# 切换窗口
driver.switch_to.window(handle)
sleep(5)
# 点击加入购物车
driver.find_element_by_xpath("//div[@class='itemInfo-wrap']/div/div/a[contains(@onclick,'加入购物车')]").click()
# 调用driver的page_source属性获取页面源码
pageSource = driver.page_source
# 断言页面源码中是否包含“商品已成功加入购物车”关键字,以此判断页面内容是否正确
assert "商品已成功加入购物车" in pageSource
print("商品已成功加入购物车") def payOrder():
# # 获取当前窗口句柄
current_handle = driver.current_window_handle
# 打印当前窗口句柄
print(current_handle)
print("点击购物车")
# 点击“我的购物车”
driver.find_element_by_xpath("//a[text()='我的购物车']").click()
sleep(2)
all_handles = driver.window_handles
# 循环遍历所有新打开的窗口句柄,也就是说不包括主窗口
for handle in all_handles:
if handle != current_handle:
# 切换窗口
driver.switch_to.window(handle)
sleep(5)
# 点击“去结算”button
driver.find_element_by_xpath("//div[@id='cart-floatbar']/div/div/div/div[2]/div[4]/div[1]/div/div[1]").click()
# driver.find_element_by_xpath("//a[contains(text(),'去结算')]").click()
sleep(2)
# 点击“提交订单”button
driver.find_element_by_xpath("//button[@id='order-submit']").click()
# 调用driver的page_source属性获取页面源码
pageSource = driver.page_source
# 断言页面源码中是否包含“商品已成功加入购物车”关键字,以此判断页面内容是否正确
assert "订单提交成功,请尽快付款" in pageSource if __name__=="__main__":
login_cookie()
get_url_with_cookies()
shopping()
payOrder()

python+selenium实现经京东登录+购物+支付的更多相关文章

  1. python+selenium自动化禅道登录测试

    本文以禅道登录测试为例,思路主要分openBrowser.openUrl.findElement.sendVals.checkResult.ReadUserdate六部分 openBrowser de ...

  2. 4、python+selenium实现12306模拟登录

    简介: 这里是利用了selenium+图片识别验证,来实现12306的模拟登录,中间也参考了好几个项目,实现了这个小demo,中间也遇到了很多的坑,主要难点在于图片识别和滑动验证这两个方面,图片识别是 ...

  3. python+selenium+chrome实现自动登录百度

    #python3.4+selenium3.5+chrome版本 63.0.3239.132+chrome驱动chromedriver.exe #实现自动登录百度 from selenium impor ...

  4. python selenium chrome 实现自动化登录

    1.环境安装 selenium的开发文档网址(英语好的可以直接看这个,写的很详细):http://selenium-python.readthedocs.io/ 因为实现的时候使用的是谷歌浏览器,在运 ...

  5. Python+Selenium - windows安全中心的弹窗(账号登录)

    当出现如下图所示的 Windows安全中心弹窗,需要输入用户名和密码时 如何用Python+selenium跳过这个登录. 步骤: 1.在注册表中三个位置各添加两个东西:iexplore.exe 和 ...

  6. python+selenium模拟京东登录后台

    python+selenium模拟京东登录后台 import json from time import sleep from selenium import webdriver #from sele ...

  7. 一次完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试

    Web登录测试是很常见的测试!手动测试大家再熟悉不过了,那如何进行自动化登录测试呢!本文作者就用python+selenium结合unittest单元测试框架来进行一次简单但比较完整的cnblog自动 ...

  8. Python + Selenium 实现登录Office 365

    最近捡起之前用的Python + Selenium实现工作中需要的登录Office 365功能.(吐槽:国内网络真是卡,登录Office 365实属不易.另外Selenium这样的网站都要墙,无法理解 ...

  9. selenium+python自动化测试系列(一):登录

        最近接手商城的项目,针对后台测试,功能比较简单,但是流程比较繁多,涉及到前后台的交叉测试.在对整个项目进行第一轮测试完成之后,考虑以后回归测试任务比较重,为了减轻回归测试的工作量,所以考虑后台 ...

随机推荐

  1. fiddler之数据分析和查看(inspectors)-抓包

    在instpectors中主要是对请求和响应进行查看和分享,监听请求的响应内容.他有多个分页标签.界面分上下两部分,上面部分显示请求的相关信息:下面部分显示响应相关信息.简单说明常用的几个分页标签 一 ...

  2. PyQt5学习一---环境的安装和配置

    PyQt5环境安装 1.Python环境(我在练习的时候是用的Python3.6.8) 2.PyQt5安装 首先安装sip pip install sip 然后安装PyQt5-tools pip in ...

  3. Selenium学习之==>ActionChainsApi接口详解

    ActionChains UI自动化测试过程中,经常遇到那种,需要鼠标悬浮后,要操作的才会元素出现的这种场景,那么我们就要模拟鼠标悬浮到某一个位置,做一系列的连贯操作,Selenium给我们提供了Ac ...

  4. 【MyBatis】----【MyBatis】--封装---别名---properties

    一.核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration ...

  5. c++ 调用 sqlcipher

    #include <iostream> #include <string.h> #include "sqlite3.h" using namespace s ...

  6. 同时安装 Python2 & Python3 cmd下版本自由选择

    系统:win7 python2.7,python3.6同时安装,于是问题来了,python27与python36文件夹下的文件名都是python.exe 这样在cmd下,直接输入python,自动执行 ...

  7. Our growth depends not on how many experiences we devour, but on how manywe digest.

    rot. v/n. 腐烂 vibration.n. 震动 charcoal. n 木炭 wrinkle. v. 长皱纹 geometry. n. 几何学 walnut.n. 核桃 tailor. n. ...

  8. MathType的配置问题;将word中的公式转换为mathtype格式失败,缺少OMML2MML.XSL

    安装MathType后打开word报错 打开会出现以下问题: 首先,把startup添加到word的信任中心: 要确保路径被office信任.依次打开word->文件->选项->信任 ...

  9. yii日志保存机制

    一.修改yii框架的配置文件(main.php) 'log' => array( 'class' => 'CLogRouter', 'routes' => array( array( ...

  10. 使用electron实现百度网盘悬浮窗口功能!

    相关依赖 里面使用了vuex vue vue-route storeJs storeJs 用来持久化vuex状态 展示 介绍说明 没有使用electron内置的-webkit-app-region: ...