# coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time,os
base_url =raw_input(u"要操作的网站:")
chromedriver = "******************" #调用chrome安装文件夹下的驱动文件(驱动需要下载)
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
def login(phone='***********'):
driver.get(base_url + "下级目录")
driver.find_element_by_id("ID").clear()
driver.find_element_by_id("ID").send_keys(phone)
driver.find_element_by_id("ID").clear()
driver.find_element_by_id("ID").send_keys("*******")
driver.find_element_by_id("ID").click()
def method():
while(True):
time.sleep(2)
driver.get(base_url+"下级目录")
time.sleep(1)
Select(driver.find_element_by_id("ID")).select_by_value("15")#通过ID定位页面元素(下拉框选中)
driver.find_element_by_id("ID").click() #<span style="font-family: Arial, Helvetica, sans-serif;">通过ID定位页面元素(点击按钮)</span>
try:
driver.find_element_by_xpath("//div[@id='ID']/table/tbody/tr[1]/td[2]/a").click()#通过xpath定位元素
time.sleep(1)
driver.find_element_by_id("ID").click()
close_alert(2)
except:
print "Already Done it "
driver.quit()
break
def close_alert(num=1): #定位到对话框并关闭对话框
for i in range(0,num):
time.sleep(1)
a = driver.switch_to_alert() #获取对话框对象
driver.implicitly_wait(5)
a.accept() #点击“确认”
time.sleep(1)
if __name__ == "__main__":
login()
method()
学习自动化是个很有趣的事,相信自己,持之以恒
python 自动化测试
最新推荐文章于 2018-11-04 17:15:38 发布