selenium自动化测试全流程
一 代码依赖库
import time
import sys
# 创建浏览器实例
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import os
二 登录验证
options = Options()
# options.add_argument('--headless')
options.add_argument('--no-sandbox')
# options.add_argument('--disable-dev-shm-usage')
# 这个路径要改成你下载的的chromedriver的存放地址
service = Service(executable_path="xxx/dive/chromedriver")
driver = webdriver.Chrome(options, service)
driver.implicitly_wait(30) #全局软依赖
driver.get("xxxxx")
time.sleep(2)
# 打开网址 login
#el-input__inner
#"name", "id", "class name", "xpath", "css selector"
username_password = driver.find_elements("class name", "el-input__inner")
username,password = username_password[0],username_password[1]
username.send_keys("xxxxx")
password.send_keys("xxxxx")
#sloid 滑动块验证 移动距离
distance = 332-58
distance = distance + 12
wait = WebDriverWait(driver, 40)
slider = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'el-icon-d-arrow-right')))
ActionChains(driver).click_and_hold(slider).perform()
ActionChains(driver).move_by_offset(xoffset=distance, yoffset=0).perform()
time.sleep(2)
ActionChains(driver).release().perform()
#in searcher
login = driver.find_element(By.CSS_SELECTOR, "a")
login.click()
三 页面跳转
注意frame跳转处理
#//div[@class='btn']/a
submit = wait.until(EC.presence_of_element_located((By.XPATH, "//div[contains(text(),'专题应用')]")))
submit.click()
time.sleep(1)
submit = driver.find_element(By.XPATH, "//div[contains(text(),'居民海岛')]")
submit.click()
time.sleep(1)
submit = driver.find_element(By.XPATH, "//li[contains(text(),'海岛开发利用')]")
submit.click()
#等待输入框出现
time.sleep(20)
# 无居民海岛开发利用现状
submit = driver.find_elements(By.XPATH, "//span[contains(text(),'开发利用现状')]")
submit[2].click()
time.sleep(1)
#input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'input[placeholder="请输入海岛编号"]')))
word_path = "处理目录"
for fileNumber in os.listdir(word_path):
file_path = os.path.join(word_path, fileNumber)
driver.switch_to.frame(1)
inputs = driver.find_element(By.XPATH, '//input[@placeholder="请输入编号"]')
inputs.clear()
inputs.send_keys(fileNumber)
search = driver.find_element(By.XPATH, "//span[contains(text(),'查询')]")
search.click()
# 获取当前窗口的句柄
original_window = driver.current_window_handle
time.sleep(1)
# 触发打开新窗口的操作
# 假设页面上有一个链接可以打开新窗口
button= driver.find_elements(By.XPATH, "//span[contains(text(),'编辑')]")
button[1].click()
# 等待新窗口打开
time.sleep(2)
# 获取所有窗口的句柄
all_windows = driver.window_handles
# 切换到新窗口
for window in all_windows:
if window != original_window:
driver.switch_to.window(window)
break
四 页面处理
页面等待中依据文件大小估算等待时间
nextPage = driver.find_element(By.XPATH, "//span[contains(text(),'下一步')]").click()
time.sleep(1)
surePage = driver.find_elements(By.XPATH, "//button[span[contains(text(),'确定')]]")
driver.execute_script('arguments[0].click();', surePage[4])
#是
button= driver.find_element(By.XPATH, "//*[@id='pane-UploadAnnex']/div/div[1]/div[2]/div[2]/div/form/div/div[1]/div/div/div/label[1]/span[1]")
button.click()
yes_path = os.path.join(file_path, "Y")
# pdf 结尾为 佐证材料 jpg 为 现状图
for upFile in os.listdir(yes_path):
if os.path.splitext(upFile)[1] == '.pdf':
button= driver.find_element(By.XPATH, '//*[@id="pane-UploadAnnex"]/div/div[1]/div[2]/div[2]/div/form/div/div[3]/div/div/div/div/input')
button.click()
button= driver.find_element(By.XPATH, "//li[span[contains(text(),'佐证材料')]]")
button.click()
elif os.path.splitext(upFile)[1] == '.jpg':
button= driver.find_element(By.XPATH, '//*[@id="pane-UploadAnnex"]/div/div[1]/div[2]/div[2]/div/form/div/div[3]/div/div/div/div/input')
button.click()
button= driver.find_element(By.XPATH, "//li[span[contains(text(),'现状图')]]")
button.click()
elif "现场照片" in upFile:
true_fold = os.path.join(yes_path, upFile)
for one in os.listdir(true_fold):
true_file = os.path.join(true_fold, one)
button= driver.find_element(By.XPATH, '//*[@id="pane-UploadAnnex"]/div/div[1]/div[2]/div[2]/div/form/div/div[3]/div/div/div/div/input')
button.click()
button= driver.find_element(By.XPATH, "//li[span[contains(text(),'现场照片')]]")
button.click()
upload_select = driver.find_element(By.XPATH, '//*[@id="pane-UploadAnnex"]/div/div[1]/div[2]/div[2]/div/form/div/div[5]/div/div/div/div/div/input')
upload_select.send_keys(true_file)
waitTime = 3.5*os.path.getsize(true_file)/(1024*1024)+8
time.sleep(waitTime)
continue
else:
continue
upload_select = driver.find_element(By.XPATH, '//*[@id="pane-UploadAnnex"]/div/div[1]/div[2]/div[2]/div/form/div/div[5]/div/div/div/div/div/input')
# 关闭新窗口
true_file = os.path.join(yes_path, upFile)
#计算等待时间
waitTime = 3.5*os.path.getsize(true_file)/(1024*1024)+8
upload_select.send_keys(true_file)
time.sleep(waitTime)
五 页面跳回
driver.close()
# 切换回原窗口
driver.switch_to.window(original_window)
time.sleep(1)
# 关闭浏览器
driver.quit()