python+selenium实现经京东登录+购物+支付
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实现经京东登录+购物+支付的更多相关文章
- python+selenium自动化禅道登录测试
本文以禅道登录测试为例,思路主要分openBrowser.openUrl.findElement.sendVals.checkResult.ReadUserdate六部分 openBrowser de ...
- 4、python+selenium实现12306模拟登录
简介: 这里是利用了selenium+图片识别验证,来实现12306的模拟登录,中间也参考了好几个项目,实现了这个小demo,中间也遇到了很多的坑,主要难点在于图片识别和滑动验证这两个方面,图片识别是 ...
- python+selenium+chrome实现自动登录百度
#python3.4+selenium3.5+chrome版本 63.0.3239.132+chrome驱动chromedriver.exe #实现自动登录百度 from selenium impor ...
- python selenium chrome 实现自动化登录
1.环境安装 selenium的开发文档网址(英语好的可以直接看这个,写的很详细):http://selenium-python.readthedocs.io/ 因为实现的时候使用的是谷歌浏览器,在运 ...
- Python+Selenium - windows安全中心的弹窗(账号登录)
当出现如下图所示的 Windows安全中心弹窗,需要输入用户名和密码时 如何用Python+selenium跳过这个登录. 步骤: 1.在注册表中三个位置各添加两个东西:iexplore.exe 和 ...
- python+selenium模拟京东登录后台
python+selenium模拟京东登录后台 import json from time import sleep from selenium import webdriver #from sele ...
- 一次完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试
Web登录测试是很常见的测试!手动测试大家再熟悉不过了,那如何进行自动化登录测试呢!本文作者就用python+selenium结合unittest单元测试框架来进行一次简单但比较完整的cnblog自动 ...
- Python + Selenium 实现登录Office 365
最近捡起之前用的Python + Selenium实现工作中需要的登录Office 365功能.(吐槽:国内网络真是卡,登录Office 365实属不易.另外Selenium这样的网站都要墙,无法理解 ...
- selenium+python自动化测试系列(一):登录
最近接手商城的项目,针对后台测试,功能比较简单,但是流程比较繁多,涉及到前后台的交叉测试.在对整个项目进行第一轮测试完成之后,考虑以后回归测试任务比较重,为了减轻回归测试的工作量,所以考虑后台 ...
随机推荐
- Unity Audio Source Properties
Audio Clip 音频剪辑 将播放声音的剪辑文件 Mute 静音 Bypass Effects 直通效果 应用音频源的快速“直通”过滤效果.一个简单的方法来打开/关闭所有特效 Output 产量 ...
- Python学习之==>常用字符串方法
1.常用字符串方法 a = '\n 字 符 串 \n\n' b = a.strip() # 默认去掉字符串两边的空格和换行符 c = a.lstrip() # 默认去掉字符串左边的空格和换行符 d = ...
- 【算法与数据结构】图的最小生成树 MST - Prim 算法
Prim 算法属于贪心算法. #include <stdio.h> #define VERTEXNUM 7 #define INF 10000 typedef struct Graph { ...
- 来自数据源的 String 类型的给定值不能转换为指定目标列的类型 nvarchar
.TrimEnd() 怀疑是否SqlBulkCopy是否存在某种bug,故而在系统中改写代码,用单个sql的插入数据方式,用循环逐条导入.结果是没问题.难道真的是SqlBulkCopy有某种bug?上 ...
- Chapter02 第三节 其他语句
2.3 其他语句 // getinfo.cpp #include <bits/stdc++.h> using namespace std; int main() { int carrots ...
- kubernetes快速应用入门
kubectl 就是 api server的客户端工具 创建一个nginx的pod [root@master ~]# kubectl run nginx-deploy --image=nginx:1. ...
- strtoul()要优于atoi()函数---C语言
strtoul():将字符串转为长整型整数 atoi():将字符串转为整型整数 在32位STM32中,int是32位的,如果字符串是“3123456789”,大于0x7fff fff,用atoi()函 ...
- 56 道高频 JavaScript 与 ES6+ 的面试题及答案
56 道高频 JavaScript 与 ES6+ 的面试题及答案 :https://segmentfault.com/a/1190000020082089?utm_source=weekly& ...
- [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)
[Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数. ...
- [luogu5339] [TJOI2019]唱、跳、rap和篮球(容斥原理+组合数学)(不用NTT)
[luogu5339] [TJOI2019]唱.跳.rap和篮球(容斥原理+组合数学)(不用NTT) 题面 略 分析 首先考虑容斥,求出有i堆人讨论的方案. 可以用捆绑法,把每堆4个人捆绑成一组,其他 ...