0
点赞
收藏
分享

微信扫一扫

Python+Selenium+Chrome 自动化测试TPshop商城项目实战- 登录TPShop商城_添加收货地址_ 修改⽤户头像

在觉 2022-03-30 阅读 338
# 登录TPShop商城_添加收货地址(前提:该地址不存在),然后修改⽤户头像
import selenium
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from time import sleep

#实例化浏览器对象:类名()
Chromedriver = selenium.webdriver.Chrome()
#实例化鼠标对象
actionChains = ActionChains(Chromedriver)
Chromedriver.implicitly_wait(20) #隐式等待
url = "http://www.myshop.com"
Chromedriver.get(url)
Chromedriver.maximize_window()
Chromedriver.find_element(By.LINK_TEXT, '登录').click()
Chromedriver.find_element(By.ID, 'username').send_keys('15700000001') #用户名
Chromedriver.find_element(By.ID, 'password').send_keys('123456') #密码
Chromedriver.find_element(By.ID, 'verify_code').send_keys('8888') #验证码
Chromedriver.find_element(By.CLASS_NAME, 'J-login-submit').click() #点击登录
actionChains.move_to_element(Chromedriver.find_element(By.CLASS_NAME, "u-dt")).perform()  #鼠标悬停账户设置
Chromedriver.find_element(By.LINK_TEXT, '收货地址').click() #点击收货地址
Chromedriver.find_element(By.CLASS_NAME, "co_blue").click() #点击增加新地址
sleep(3)
Chromedriver.switch_to.frame(Chromedriver.find_element(By.TAG_NAME, 'iframe')) #填写地址弹窗为iframe,需进行切换
Chromedriver.find_element(By.NAME, "consignee").send_keys("收货人") #输入收货人
select_province = Select(Chromedriver.find_element(By.ID, 'province')) #下拉选择省份
select_province.select_by_visible_text("北京市") #使用文本
sleep(2)
select_province = Select(Chromedriver.find_element(By.ID, 'city')) #下拉选择市
select_province.select_by_visible_text("市辖区")
sleep(2)
select_province = Select(Chromedriver.find_element(By.ID, 'district')) #下拉选择区
select_province.select_by_visible_text("东城区")
sleep(2)
select_province = Select(Chromedriver.find_element(By.ID, 'twon')) #下拉选择镇或社区
select_province.select_by_visible_text("东华门街道")
sleep(2)
Chromedriver.find_element(By.CSS_SELECTOR, '[placeholder="详细地址"]').send_keys('北京市东城区东华门街道') #输入详细地址
Chromedriver.find_element(By.NAME, 'zipcode').send_keys('100000') #输入邮编
Chromedriver.find_element(By.NAME, 'mobile').send_keys('15700000001') #输入电话号码
Chromedriver.find_element(By.TAG_NAME, 'button').click() #点击保存收货地址
sleep(5)
Chromedriver.switch_to.default_content() #切回默认界面
actionChains.move_to_element(Chromedriver.find_element(By.CLASS_NAME, "u-dt")).perform()  #鼠标悬停账户设置
Chromedriver.find_element(By.LINK_TEXT, '个人信息').click() #点击个人信息
Chromedriver.find_element(By.XPATH, '//*[@id="preview"]').click() #点击头像图片处
Chromedriver.switch_to.frame(Chromedriver.find_element(By.TAG_NAME, 'iframe')) #上传图片弹窗为iframe,需进行切换
#使用file类型的input实现图片文件的上传时,可以直接利用Selenium提供的send_keys()方法实现文件上传,传入参数为本地图片存放地址
Chromedriver.find_element(By.XPATH, '//html/body/div/div/div[1]/div/div[2]/div/div/div[2]/input').send_keys('D:\picture.jpg') #
sleep(5) #等待图片上传完成
Chromedriver.find_element(By.CLASS_NAME, 'saveBtn').click() #点击确认使用
Chromedriver.switch_to.default_content() #切回默认界面
# 点击确定保存
Chromedriver.find_element(By.XPATH,'/html/body/div[3]/div/div[2]/div[2]/div/div[2]/form/ul[5]/li[2]/div[3]/input').click()
sleep(10) #等待保存完成自动跳转
Chromedriver.quit()  # 关闭退出
举报

相关推荐

[Python]Selenium-自动化测试

0 条评论