白月黑羽教python之selenium:课后练习
程序员文章站
2022-05-22 08:00:15
...
本博客记录了白月黑羽教python的selenium课后作业的代码,方便以后查看,希望可以帮助看到这篇博客的人,最后感谢白月黑羽老师的免费教程,讲的非常清楚!
from selenium import webdriver
from time import sleep
# 创建 WebDriver 实例对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(r'I:\chromedriver_win32\chromedriver.exe')
# WebDriver 实例对象的get方法 可以让浏览器打开指定网址
wd.get('http://127.0.0.1/mgr/sign.html')
# 设置最大等待时长为 10秒
wd.implicitly_wait(10)
#最大化窗口
wd.maximize_window()
#登录白月黑羽系统
elementuser = wd.find_element_by_id('username')
elementuser.send_keys('byhy')
elementpass = wd.find_element_by_id('password')
elementpass.send_keys('88888888')
elementbutton=wd.find_element_by_tag_name('button')
elementbutton.click()
#定位侧边菜单栏
elementsidebar=wd.find_element_by_class_name('sidebar')
elementlist=elementsidebar.find_elements_by_tag_name('span')
for element in elementlist:
print(element.text)
from selenium import webdriver
from time import sleep
# 创建 WebDriver 实例对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(r'I:\chromedriver_win32\chromedriver.exe')
# WebDriver 实例对象的get方法 可以让浏览器打开指定网址
wd.get('http://127.0.0.1/mgr/sign.html')
# 设置最大等待时长为 10秒
wd.implicitly_wait(10)
#最大化窗口
wd.maximize_window()
#登录白月黑羽系统
elementuser = wd.find_element_by_id('username')
elementuser.send_keys('byhy')
elementpass = wd.find_element_by_id('password')
elementpass.send_keys('88888888')
elementbutton=wd.find_element_by_tag_name('button')
elementbutton.click()
#创建客户
elementadd=wd.find_element_by_class_name('col-lg-12')
kehu=elementadd.find_element_by_tag_name('button')
kehu.click()
build=wd.find_element_by_class_name('col-lg-8')
inputs=build.find_elements_by_tag_name('input')
inputs[0].send_keys('南京中医院')
inputs[1].send_keys('12345678')
input2=build.find_element_by_tag_name('textarea')
input2.send_keys('地址我不知道啊')
buildbut=elementadd.find_element_by_class_name('col-lg-12')
buildbuttons=buildbut.find_elements_by_tag_name('button')
buildbuttons[0].click()
#修改客户地址,不要问为什么不修改客户名,懒得改了,当时看错了,哈哈哈
sleep(2)
result=wd.find_element_by_class_name('search-result-item')
resultbut=result.find_element_by_class_name('search-result-item-actionbar')
resultbuttons=resultbut.find_elements_by_class_name('btn-green')
resultbuttons[0].click()#点击[编辑]按钮
adress=result.find_element_by_tag_name('textarea')
adress.clear()
adress.send_keys('南京省中医院')
resultbuttons[0].click()
from selenium import webdriver
from time import sleep
# 创建 WebDriver 实例对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(r'I:\chromedriver_win32\chromedriver.exe')
# WebDriver 实例对象的get方法 可以让浏览器打开指定网址
wd.get('http://127.0.0.1/mgr/sign.html')
# 设置最大等待时长为 10秒
wd.implicitly_wait(10)
#最大化窗口
wd.maximize_window()
#登录白月黑羽系统
elementuser = wd.find_element_by_id('username')
elementuser.send_keys('byhy')
elementpass = wd.find_element_by_id('password')
elementpass.send_keys('88888888')
elementbutton=wd.find_element_by_tag_name('button')
elementbutton.click()
#创建客户css
wd.find_element_by_css_selector('.col-lg-12>button').click()
infors=wd.find_elements_by_css_selector('input.form-control')
infors[0].send_keys('css')
infors[1].send_keys('12312121222')
wd.find_element_by_css_selector('textarea').send_keys('css123')
wd.find_element_by_css_selector('button.btn.btn-green.btn-outlined.btn-xs').click()
#修改客户地址css
sleep(2)
wd.find_element_by_css_selector('label.btn-green.btn-outlined.btn-xs').click()
wd.find_element_by_css_selector('.search-result-item textarea.form-control').clear()
wd.find_element_by_css_selector('#root > div > section.content.container-fluid > div:nth-child(3) > div:nth-child(1) > div:nth-child(3) > textarea').send_keys('定位修改之后')
wd.find_element_by_css_selector('#root > div > section.content.container-fluid > div:nth-child(3) > div.search-result-item-actionbar > div > label:nth-child(1)').click()
from selenium import webdriver
from time import sleep
# 创建 WebDriver 实例对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(r'I:\chromedriver_win32\chromedriver.exe')
# WebDriver 实例对象的get方法 可以让浏览器打开指定网址
wd.get('http://127.0.0.1/mgr/sign.html')
# 设置最大等待时长为 10秒
wd.implicitly_wait(10)
#最大化窗口
wd.maximize_window()
#登录白月黑羽系统
elementuser = wd.find_element_by_id('username')
elementuser.send_keys('byhy')
elementpass = wd.find_element_by_id('password')
elementpass.send_keys('88888888')
elementbutton=wd.find_element_by_tag_name('button')
elementbutton.click()
#点击【药品】按钮css
wd.find_element_by_css_selector('[href="#/medicines"]').click()
sleep(2)
#点击【添加药品】按钮css
wd.find_element_by_css_selector('.col-lg-12>button').click()
wd.find_element_by_css_selector('.col-lg-8>:nth-child(1)>input').send_keys('medicine')
wd.find_element_by_css_selector('.col-lg-8>:nth-child(2)>input').send_keys('21112')
wd.find_element_by_css_selector('.col-lg-8>:nth-child(3)>textarea').send_keys('乱加的啊啊啊啊')
wd.find_element_by_css_selector('.col-lg-12>.btn-xs:nth-child(1)').click()
from selenium import webdriver
from time import sleep
# 创建 WebDriver 实例对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(r'I:\chromedriver_win32\chromedriver.exe')
# WebDriver 实例对象的get方法 可以让浏览器打开指定网址
wd.get('http://127.0.0.1/mgr/sign.html')
# 设置最大等待时长为 10秒
wd.implicitly_wait(10)
#最大化窗口
wd.maximize_window()
#登录白月黑羽系统
elementuser = wd.find_element_by_id('username')
elementuser.send_keys('byhy')
elementpass = wd.find_element_by_id('password')
elementpass.send_keys('88888888')
elementbutton=wd.find_element_by_tag_name('button')
elementbutton.click()
#点击外链之前先保存本页面句柄
mainwindow=wd.current_window_handle
#点击外链
wd.find_element_by_css_selector('.pull-right>[href="http://www.python3.vip"]').click()
#寻找所选外链
for handle in wd.window_handles:
wd.switch_to.window(handle)
if '白月黑羽教Python' in wd.title:
break
wd.maximize_window()
barelements=wd.find_elements_by_css_selector('li.nav-item span')
for barelement in barelements:
print(barelement.text)
wd.switch_to.window(mainwindow)
wd.find_element_by_css_selector('span.hidden-xs').click()
wd.find_element_by_css_selector('.pull-right a.btn').click()
sleep(2)
if wd.current_url=="http://127.0.0.1/mgr/sign.html":
print("成功退出登录")
from selenium import webdriver
from time import sleep
# 导入Select类
from selenium.webdriver.support.ui import Select
# 创建 WebDriver 实例对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(r'I:\chromedriver_win32\chromedriver.exe')
# WebDriver 实例对象的get方法 可以让浏览器打开指定网址
wd.get('http://127.0.0.1/mgr/sign.html')
# 设置最大等待时长为 10秒
wd.implicitly_wait(10)
#最大化窗口
wd.maximize_window()
#登录白月黑羽系统
elementuser = wd.find_element_by_id('username')
elementuser.send_keys('byhy')
elementpass = wd.find_element_by_id('password')
elementpass.send_keys('88888888')
elementbutton=wd.find_element_by_tag_name('button')
elementbutton.click()
#在系统中添加3种药品
wd.find_element_by_css_selector('a[href="#/medicines"]> span').click()
wd.find_element_by_css_selector('.col-lg-12 button .glyphicon').click()
wd.find_element_by_css_selector('.col-lg-8 div:nth-child(1) input').send_keys('青霉素盒装1')
wd.find_element_by_css_selector('.col-lg-8 div:nth-child(2) input').send_keys('YP-32342341')
wd.find_element_by_css_selector('.col-lg-8 div:nth-child(3) textarea').send_keys('青霉素注射液,每支15ml,20支装')
wd.find_element_by_css_selector('button.btn-xs:nth-child(1)').click()
sleep(2)
wd.find_element_by_css_selector('.col-lg-8 div:nth-child(1) input').send_keys('青霉素盒装2')
wd.find_element_by_css_selector('.col-lg-8 div:nth-child(2) input').send_keys('YP-32342342')
wd.find_element_by_css_selector('.col-lg-8 div:nth-child(3) textarea').send_keys('青霉素注射液,每支15ml,30支装')
wd.find_element_by_css_selector('button.btn-xs:nth-child(1)').click()
sleep(2)
wd.find_element_by_css_selector('.col-lg-8 div:nth-child(1) input').send_keys('青霉素盒装3')
wd.find_element_by_css_selector('.col-lg-8 div:nth-child(2) input').send_keys('YP-32342343')
wd.find_element_by_css_selector('.col-lg-8 div:nth-child(3) textarea').send_keys('青霉素注射液,每支15ml,40支装')
wd.find_element_by_css_selector('button.btn-xs:nth-child(1)').click()
#添加3个客户
wd.find_element_by_css_selector('[href="#/customers"] span').click()
sleep(2)
wd.find_element_by_css_selector('.col-lg-12>button').click()
infors=wd.find_elements_by_css_selector('input.form-control')
infors[0].send_keys('南京中医院1')
infors[1].send_keys('2551867851')
wd.find_element_by_css_selector('textarea').send_keys('江苏省-南京市-秦淮区-汉中路-501')
wd.find_element_by_css_selector('button.btn.btn-green.btn-outlined.btn-xs').click()
sleep(2)
infors[0].send_keys('南京中医院2')
infors[1].send_keys('2551867852')
wd.find_element_by_css_selector('textarea').send_keys('江苏省-南京市-秦淮区-汉中路-502')
wd.find_element_by_css_selector('button.btn.btn-green.btn-outlined.btn-xs').click()
sleep(2)
infors[0].send_keys('南京中医院3')
infors[1].send_keys('2551867853')
wd.find_element_by_css_selector('textarea').send_keys('江苏省-南京市-秦淮区-汉中路-503')
wd.find_element_by_css_selector('button.btn.btn-green.btn-outlined.btn-xs').click()
sleep(2)
#添加订单
wd.find_element_by_css_selector('[href="#/orders"] span').click()
wd.find_element_by_css_selector('.col-lg-12 span').click()
wd.find_element_by_css_selector('.col-lg-8 div:nth-child(1) input').send_keys('订单1')
#创建Select对象
select1 = Select(wd.find_element_by_css_selector(".col-lg-8 div:nth-child(2) .xxx"))
sleep(1)
select1.select_by_visible_text('南京中医院2')
select2 = Select(wd.find_element_by_css_selector(".col-lg-8 div:nth-child(3) .xxx"))
sleep(1)
select2.select_by_visible_text('青霉素盒装1')
wd.find_element_by_css_selector("input[type='number']").send_keys('100')
wd.find_element_by_css_selector('button.btn-xs:nth-child(1)').click()
from selenium import webdriver
from time import sleep
# 导入Select类
from selenium.webdriver.support.ui import Select
# 创建 WebDriver 实例对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(r'I:\chromedriver_win32\chromedriver.exe')
# WebDriver 实例对象的get方法 可以让浏览器打开指定网址
wd.get('http://127.0.0.1/mgr/sign.html')
# 设置最大等待时长为 10秒
wd.implicitly_wait(10)
#最大化窗口
wd.maximize_window()
#登录白月黑羽系统
elementuser = wd.find_element_by_id('username')
elementuser.send_keys('byhy')
elementpass = wd.find_element_by_id('password')
elementpass.send_keys('88888888')
elementbutton=wd.find_element_by_tag_name('button')
elementbutton.click()
#删除已有订单alert
wd.find_element_by_xpath('//a[@href="#/orders"]//span').click()
sleep(2)
orders_had=wd.find_elements_by_xpath('//div[@class="search-result-item"]')
if orders_had:
for order_had in orders_had:
sleep(2)
order_had.find_element_by_xpath('./div[5]//label').click()
sleep(2)
wd.switch_to.alert.accept()
#在系统中添加3种药品xpath
sleep(2)
wd.find_element_by_xpath('//a[@href="#/medicines"]//span').click()
#没订单有药品时,删除药品
medicines=wd.find_elements_by_xpath('//div[@class="search-result-item-actionbar"]')
if medicines:
for medicine in medicines:
sleep(2)
medicine.find_element_by_xpath('.//label[2]').click()
sleep(2)
wd.switch_to.alert.accept()
sleep(2)
wd.find_element_by_xpath('//div[@class="col-lg-12 col-md-12 col-sm-12 add-one-area"]/button').click()
sleep(2)
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[1]/input').send_keys('青霉素盒装1')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[2]/input').send_keys('YP-32342341')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[3]/textarea').send_keys('青霉素注射液,每支15ml,20支装')
wd.find_element_by_xpath('//div[@class="col-lg-12 col-md-12 col-sm-12"]/button[1]').click()
sleep(2)
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[1]/input').send_keys('青霉素盒装2')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[2]/input').send_keys('YP-32342342')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[3]/textarea').send_keys('青霉素注射液,每支15ml,30支装')
wd.find_element_by_xpath('//div[@class="col-lg-12 col-md-12 col-sm-12"]/button[1]').click()
sleep(2)
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[1]/input').send_keys('青霉素盒装3')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[2]/input').send_keys('YP-32342343')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[3]/textarea').send_keys('青霉素注射液,每支15ml,40支装')
wd.find_element_by_xpath('//div[@class="col-lg-12 col-md-12 col-sm-12"]/button[1]').click()
#在系统中添加客户xpath
sleep(2)
wd.find_element_by_xpath('//a[@href="#/customers"]//span').click()
#没订单有客户时,删除客户
customers=wd.find_elements_by_xpath('//div[@class="search-result-item-actionbar"]')
if customers:
for customer in customers:
sleep(2)
customer.find_element_by_xpath('.//label[2]').click()
sleep(2)
wd.switch_to.alert.accept()
sleep(2)
wd.find_element_by_xpath('//div[@class="col-lg-12 col-md-12 col-sm-12 add-one-area"]/button').click()
sleep(2)
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[1]/input').send_keys('南京中医院1')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[2]/input').send_keys('2551867851')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[3]/textarea').send_keys('江苏省-南京市-秦淮区-汉中路-501')
wd.find_element_by_xpath('//div[@class="col-lg-12 col-md-12 col-sm-12"]/button[1]').click()
sleep(2)
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[1]/input').send_keys('南京中医院2')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[2]/input').send_keys('2551867852')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[3]/textarea').send_keys('江苏省-南京市-秦淮区-汉中路-502')
wd.find_element_by_xpath('//div[@class="col-lg-12 col-md-12 col-sm-12"]/button[1]').click()
sleep(2)
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[1]/input').send_keys('南京中医院3')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[2]/input').send_keys('2551867853')
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[3]/textarea').send_keys('江苏省-南京市-秦淮区-汉中路-503')
wd.find_element_by_xpath('//div[@class="col-lg-12 col-md-12 col-sm-12"]/button[1]').click()
#在系统中添加订单xpath
sleep(2)
wd.find_element_by_xpath('//a[@href="#/orders"]//span').click()
sleep(2)
wd.find_element_by_xpath('//div[@class="col-lg-12 col-md-12 col-sm-12 add-one-area"]/button').click()
sleep(2)
wd.find_element_by_xpath('//div[@class="col-lg-8 col-md-8 col-sm-8"]/div[1]/input').send_keys('xpath')
Select(wd.find_element_by_xpath('//div//div[2]//select')).select_by_visible_text('南京中医院2')
Select(wd.find_element_by_xpath('//div//div[3]//select')).select_by_visible_text('青霉素盒装1')
sleep(2)
wd.find_element_by_xpath('//input[@type="number"]').send_keys('100')
wd.find_element_by_xpath('//div[@class="col-lg-12 col-md-12 col-sm-12"]/button[1]').click()
注意:
1、如果报错selenium.common.exceptions.NoAlertPresentException: Message: no such alert
,可能是因为网页加载是异步的,导致有些节点还没有加载出来,但是代码已经在运行,所以在操作之前,先执行sleep()。
2、selenium的webdriver三种等待方式(显式等待WebDriverWait+implicitly_wait隐式等待+sleep强制等待)
https://blog.csdn.net/cyjs1988/article/details/76033180
http://www.testclass.net/selenium_python/element-wait
上一篇: 请问OCI环境初始化异常
下一篇: poi包冲突问题的实例详解