如果对xpath有疑问,可以看前面文章介绍,如果获取xpath。简单来讲,打开浏览器开发者模式,右键元素,复制xpath即可。
- #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- import time
-
- from selenium.webdriver.support.wait import WebDriverWait
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.common.action_chains import ActionChains
- from pathlib import Path
-
-
- urls = {
- 'login': 'https://***.***.***.***'
- }
-
- login_info = {
- 'username': '***',
- 'password': '***'
- }
-
- project_name = '***'
-
-
- def get_driver():
- # chromedriver是当前路径,已经下载好的chrome驱动。
- chromedriver = 'chromedriver'
- # 配置对象
- options = webdriver.ChromeOptions()
- # 取消用户名密码保存的浏览器提示
- prefs = {"profile.password_manager_enabled": False, "credentials_enable_service": False}
- # 取消用户名密码保存的浏览器提示
- options.add_experimental_option("prefs", prefs)
- # 隐藏“Chrome正受到自动测试软件的控制。”
- options.add_experimental_option('excludeSwitches', ['enable-automation'])
- driver = webdriver.Chrome(executable_path=chromedriver, options=options)
- # 隐式等0.5秒
- driver.implicitly_wait(0.5)
- driver.maximize_window()
- return driver
-
-
- def login(driver):
- # 自动登录UC
- driver.get(urls['login'])
- # 定位用户名输入框
- username = driver.find_element(By.NAME, value='tbxUserName')
- # 清空输入框的内容
- username.clear()
- # 输入用户名
- username.send_keys(login_info['username'])
- # 定位密码输入框
- password = driver.find_element(By.NAME, value='tbxPassword')
- # 输入密码
- password.send_keys(login_info['password'])
-
- # 如果有验证码,可以使用下面方法
- # captcha_xpath = ''
- # wait_xpath(driver, captcha_xpath)
- # captcha = driver.find_element(By.XPATH, value=captcha_xpath)
- # code = input('验证码:')
- # captcha.send_keys(code)
-
- # 在浏览器开发者工具模式下,可以通过找到你想要操作的元素(登录按钮),然后选择复制--> XPath。
- login_xpath = '//*[@id="btnLogin"]'
- driver.find_element(By.XPATH, value=login_xpath).click()
-
- pop_close_xpath = '//*[@id="form1"]/div[3]/div[1]/span'
- wait_xpath(driver, pop_close_xpath)
- driver.find_element(By.XPATH, value=pop_close_xpath).click()
- time.sleep(1)
-
-
- def display_all_project(driver):
- # 切入
- navi_pane_id = 'left_frame'
- driver.switch_to.frame(driver.find_element(By.ID, value=navi_pane_id))
- project_mgt = '//*[@id="dd0"]/div[7]/a[2]'
- driver.find_element(By.XPATH, value=project_mgt).click()
- query_all_projects = '//*[@id="sd31"]'
- driver.find_element(By.XPATH, value=query_all_projects).click()
-
-
- def input_project_name(driver, project_name):
- # 回到主
- driver.switch_to.parent_frame()
- content_frame_id = 'right_frame'
- driver.switch_to.frame(driver.find_element(By.ID, value=content_frame_id))
- project_input = '//*[@id="txtProjectName"]'
- driver.find_element(By.XPATH, value=project_input).send_keys(project_name)
-
-
- def select_option(driver):
- # 选择下拉框
- mark_xpath = '//*[@id="dropStage_txtMain"]'
- mark = driver.find_element(By.XPATH, value=mark_xpath)
- # 将鼠标指针移动到mark元素上,执行单击操作
- ActionChains(driver).move_to_element(mark).click(mark).perform()
- # 下拉框中的选项位置
- mark_index_xpath = '//*[@id="dropStage_chkItemText0"]'
- mark_index = driver.find_element(By.XPATH, value=mark_index_xpath)
- # 选择下拉框中的选项
- ActionChains(driver).move_to_element(mark_index).click(mark_index).perform()
-
-
- def click_query(driver):
- query_button = '//*[@id="btnSearch"]'
- driver.find_element(By.XPATH, value=query_button).click()
-
-
- def screenshot(driver):
- # 保存截图
- dir = Path('screenshot')
- if not dir.is_dir():
- dir.mkdir(parents=True)
- now = str(int(time.time()))
- imgpath = dir.joinpath(f'{now}.png')
- driver.save_screenshot(str(imgpath))
-
-
- def clear(driver):
- # 清空所有Cookie
- driver.delete_all_cookies()
- # 退出浏览器
- driver.quit()
-
-
- def wait_xpath(driver, xpath):
- try:
- # 显示等待10s,每秒判断元素是否加载完成
- WebDriverWait(driver, 15, 1).until(
- lambda x: x.find_element(By.XPATH, value=xpath)
- )
- except Exception as e:
- raise e
-
-
- if __name__ == '__main__':
- driver = get_driver()
- login(driver)
- display_all_project(driver)
- input_project_name(driver, project_name)
- select_option(driver)
- click_query(driver)
- screenshot(driver)
- clear(driver)