python实现登录购物_python+selenium实现经京东登录+购物+支付

importjsonfrom time importsleepfrom selenium importwebdriverimportchardetfrom selenium.webdriver importActionChains#初始化浏览器#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"

deflogin_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("18501057627")#定位密码框,并输入密码

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

defget_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 injd_cookies_dict:

driver.add_cookie(cookie)#验证是否登录成功

driver.get(url)assert '退出' indriver.page_sourceprint(url)#添加购物车

defshopping():

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 inall_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 "商品已成功加入购物车" inpageSourceprint("商品已成功加入购物车")defpayOrder():## 获取当前窗口句柄

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 inall_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 "订单提交成功,请尽快付款" inpageSourceif __name__=="__main__":

login_cookie()

get_url_with_cookies()

shopping()

payOrder()

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值