档案信息网站建设工作经验,设计制作平板电脑支架,龙岩天宫山旅游攻略,网络系统管理员在哪里场景
点击添加”新增“按钮#xff0c;弹出”新增对话框“#xff0c;输入各种数据#xff0c;然后点击”确定“按钮#xff0c;如此循环。数量多#xff0c;这样操作累人。
selenium
Selenium 是一个用于自动化 Web 浏览器操作的库#xff0c;可以实现模拟点击、输入…场景
点击添加”新增“按钮弹出”新增对话框“输入各种数据然后点击”确定“按钮如此循环。数量多这样操作累人。
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.ui import Select
import time# 目标页面
target_url https://test.abc.com/12311/application/#/application/update/2310?modeeditenvprod# 创建一个Chrome浏览器实例
#browser webdriver.Chrome(executable_pathdriver_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, .footerbutton)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 * 10print(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 的 listprint(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(uElement not interactable: %s % (msg))
except ElementNotSelectableException as msg:print(uElement not selected: %s % (msg))
except InvalidSelectorException as msg:print(uInvalid selector: %s % (msg))
except NoSuchAttributeException as msg:print(uAttribute not found: %s % (msg))
except NoSuchElementException as msg:print(uElement not found: %s % (msg))
except NoSuchFrameException as msg:print(uFrame not found: %s % (msg))
except NoSuchWindowException as msg:print(uWindow not found: %s % (msg))
except TimeoutException as msg:print(uFind 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[typetext])# 定位具有特定子元素的元素
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 则专注于通过类名来定位元素。根据需求和目标元素的特点可以选择使用适当的方法来定位页面元素。