一见

基于python+selenium的自动批量添加

场景

点击添加”新增“按钮,弹出”新增对话框“,输入各种数据,然后点击”确定“按钮,如此循环。数量多,这样操作累人。

selenium

Selenium 是一个用于自动化 Web 浏览器操作的库,可以实现模拟点击、输入文本等操作。

代码实现

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException
from selenium.common.exceptions import ElementNotSelectableException
from selenium.common.exceptions import InvalidSelectorException
from selenium.common.exceptions import NoSuchAttributeException
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoSuchFrameException
from selenium.common.exceptions import NoSuchWindowException
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
import time

# 目标页面
target_url = "https://test.abc.com/12311/application/#/application/update/2310?mode=edit&env=prod"

# 创建一个Chrome浏览器实例
#browser = webdriver.Chrome(executable_path=driver_path)
browser = webdriver.Chrome()

# 打开一个网页
browser.get(target_url)
time.sleep(0.5)

try:
    # 点击“快速登录”按钮
    print('模拟点击“快速登录”按钮')
    browser.find_element(
        By.ID, "btn_quicklogin"
    ).click()
    time.sleep(1)

    print('模拟点击“下一步”按钮')
    # 使用 class 属性定位
    button = browser.find_element(By.CSS_SELECTOR, ".footer>button")
    button.click()
    time.sleep(1)

    # 填充
    for index, metric_pair in enumerate(metric_pair_array):
        metric = metric_pair.split(':')
        print("\033[93m==============================\033[0m")
        print("(%d) %s" % (len(metric), metric_pair))
        if len(metric) == 2:
            metric_name = metric[0]
            li_element_index = index * 10
            print("metric_name:%s, index:%d, li_element_index:%d" % (metric_name, index, li_element_index))

            # 点击“新增”按钮
            browser.find_element(
                By.CSS_SELECTOR, ".table-view__operations-left>.t-button--theme-primary"
            ).click()
            time.sleep(1)

            # 中文名
            browser.find_element(By.CSS_SELECTOR, ".t-form-item__metric_chname .t-input__inner").send_keys(metric_name)

            # 英文名
            browser.find_element(By.CSS_SELECTOR, ".t-form-item__metric_enname .t-input__inner").send_keys(metric_name)

            # 类型
            print('点击“箭头”按钮')
            browser.find_element(
                By.CSS_SELECTOR, ".t-form-item__type .t-input--suffix"
            ).click()
            li_elements = browser.find_elements(By.CSS_SELECTOR, ".t-select__dropdown .t-select__list .t-size-m") # li_elements 为大小为 4 的 list
            print("type of li_elements: %s" % type(li_elements))
            if isinstance(li_elements, list): # 判断元素是否为列表类型
                print("size of li_elements: %s" % len(li_elements))
            target_li = li_elements[0 + li_element_index]
            target_li.click()

            # 单位
            print('点击“箭头”按钮')
            browser.find_element(
                By.CSS_SELECTOR, ".t-form-item__unit .t-input--suffix"
            ).click()
            li_elements = browser.find_elements(By.CSS_SELECTOR, ".t-select__dropdown .t-select__list .t-size-m") # li_elements 为大小为 10 的 list,包含了前面一步得到的 4 个
            print("type of li_elements: %s" % type(li_elements))
            if isinstance(li_elements, list):
                print("size of li_elements: %s" % len(li_elements))
                target_li = li_elements[6 + li_element_index]
                target_li.click()

            # 点击“确定”按钮
            browser.find_element(
                By.CSS_SELECTOR, ".t-dialog__footer .t-button--theme-primary"
            ).click()

            print("")
except ElementNotInteractableException as msg:
    print(u"Element not interactable: %s" % (msg))
except ElementNotSelectableException as msg:
    print(u"Element not selected: %s" % (msg))
except InvalidSelectorException as msg:
    print(u"Invalid selector: %s" % (msg))
except NoSuchAttributeException as msg:
    print(u"Attribute not found: %s" % (msg))
except NoSuchElementException as msg:
    print(u"Element not found: %s" % (msg))
except NoSuchFrameException as msg:
    print(u"Frame not found: %s" % (msg))
except NoSuchWindowException as msg:
    print(u"Window not found: %s" % (msg))
except TimeoutException as msg:
    print(u"Find element timeout: %s" % (msg))
else:
    input('按 “Enter” 键退出 ...')

# 关闭浏览器
#browser.quit()

find_element

在 Selenium 中,find_element 方法用于定位页面元素,通常与 CSS 选择器一起使用。在 CSS 选择器中,>和空格有着不同的含义:

  • .

这是一个类选择器。它选择所有具有指定类名的元素。例如 .myClass 会选择所有具有类名 myClass 的元素。

  • >

这是一个子元素选择器。它选择所有直接的子元素。例如,div > p 会选择所有 div 元素的直接子元素 p。

  • 空格

这是一个后代选择器。它选择所有的子元素,不仅仅是直接的子元素,还包括孙子元素、曾孙元素等。例如,div p 会选择 div 元素下的所有 p 元素,无论p元素是 div 的直接子元素,还是更深层次的后代元素。

这两个选择器在定位元素时非常有用,可以帮助更精确地选择想要的元素。如果目标存在多个,则应当使用 find_elements。

By.CSS_SELECTOR 和 By.CLASS_NAME

在 Selenium 中,By.CSS_SELECTOR 和 By.CLASS_NAME 都是用于定位页面元素的方法,但它们的使用方式和目标有所不同。

  • By.CSS_SELECTOR

这个方法允许使用 CSS 选择器来定位页面元素。CSS 选择器是一种强大的定位方式,可以通过元素的属性、层次结构和伪类等来定位元素。例如,可以使用 By.CSS_SELECTOR 来定位具有特定 ID、类名、属性或子元素的元素。

# 定位具有特定ID的元素
element = browser.find_element(By.CSS_SELECTOR, "#element_id")

# 定位具有特定类名的元素
element = browser.find_element(By.CSS_SELECTOR, ".element_class")

# 定位具有特定属性的元素
element = browser.find_element(By.CSS_SELECTOR, "input[type='text']")

# 定位具有特定子元素的元素
element = browser.find_element(By.CSS_SELECTOR, "div > p")
  • By.CLASS_NAME

这个方法专门用于定位具有特定类名的元素。它只能通过类名来定位元素,而不能使用其他属性或层次结构。如果只需要根据类名来定位元素,By.CLASS_NAME 是一个简单直接的选择。

# 定位具有特定类名的元素
element = browser.find_element(By.CLASS_NAME, "element_class")

总之,By.CSS_SELECTOR 提供了更多的灵活性和定位能力,而 By.CLASS_NAME 则专注于通过类名来定位元素。根据需求和目标元素的特点,可以选择使用适当的方法来定位页面元素。

posted on 2023-09-15 16:05  -见  阅读(46)  评论(0编辑  收藏  举报

导航