Selenium记录RPA初阶 - 基本输入元件

防止自己遗忘,故作此为记录。

爬取网页基本元件并修改后爬取。

包含元件:

复制代码
elements: dict[str, str] = {
    "username": None,
    "password": None,
    "email": None,
    "website": None,
    "date": None,
    "time": None,
    "number": None,
    "range": None,
    "color": None,
    "search": None
}

步骤:

(1)爬取网页原有信息:

很简单,因为都是<input>元件,只不过type不同,但大差不差。

复制代码
WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.ID, "username")))
for index, element in enumerate(elements):
    elements[element] = driver.find_element(By.ID, element).get_attribute("value")

with open("RPA_read.txt", 'w', encoding="utf-8") as file:
    file.write(pformat(elements))

(2)修改元件内容:

也简单,input元件直接send_keys即可。

复制代码
write_elements: dict[str, str] = {
    "username": "DaisyMo",
    "password": "MyLoveDaisyMo",
    "email": "[email protected]",
    "website": "DaisyWithMe.love",
    "date": "8-10",
    "time": str(time()),
    "number": "6",
    "range": "66",
    "color": "#FF8C2F",
    "search": "墨小菊"
}

for index, (element, value) in enumerate(write_elements.items()):
    #更正:需要删除之前的
    driver.find_element(By.ID, element).clear()
    driver.find_element(By.ID, element).send_keys(value)

(3)爬取修改的内容:

大差不差和上面,就是最好判断一下现在的内容和之前的是否相同。

复制代码
(
    WebDriverWait(driver, 10)
    .until(lambda driver: driver.find_element(By.ID, "username")
    .get_attribute("username") != elements["username"])
)

for index, element in enumerate(elements):
    elements[element] = driver.find_element(By.ID, element).get_attribute("value")

with open("RPA_write.txt", 'w', encoding="utf-8") as file:
    file.write(pformat(elements))

#结果就是
{'color': '#000000',
 'date': '2021-06-20',
 'email': '[email protected]',
 'number': '',
 'password': 'spiderbuf',
 'range': '50',
 'search': '',
 'time': '00:01:01',
 'username': 'Spiderbuf',
 'website': 'http://www.spiderbuf.cn'}

#之后
{'color': '#ff8c2f',
 'date': '',
 'email': '[email protected]',
 'number': '6',
 'password': 'MyLoveDaisyMo',
 'range': '50',
 'search': '墨小菊',
 'time': '17:59',
 'username': 'DaisyMo',
 'website': 'DaisyWithMe.love'}

源代码:

python 复制代码
# -*- coding: utf-8 -*-
# -*- file: RPA.py  -*-

from time import time
from pprint import pformat
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait


target_url: str = r"https://www.spiderbuf.cn/playground/n06"

service: object = ChromeService(r"RPA\chromedriver-win64\chromedriver.exe")
options: object = ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_argument("--disable-blink-features=AutomationControlled")

driver: object = Chrome(service=service, options=options)

driver.get(target_url)

elements: dict[str, str] = {
    "username": None,
    "password": None,
    "email": None,
    "website": None,
    "date": None,
    "time": None,
    "number": None,
    "range": None,
    "color": None,
    "search": None
}

write_elements: dict[str, str] = {
    "username": "DaisyMo",
    "password": "MyLoveDaisyMo",
    "email": "[email protected]",
    "website": "DaisyWithMe.love",
    "date": "8-10",
    "time": str(time()),
    "number": "6",
    "range": "66",
    "color": "#FF8C2F",
    "search": "墨小菊"
}

WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.ID, "username")))
for index, element in enumerate(elements):
    elements[element] = driver.find_element(By.ID, element).get_attribute("value")

with open("RPA_read.txt", 'w', encoding="utf-8") as file:
    file.write(pformat(elements))

for index, (element, value) in enumerate(write_elements.items()):
    driver.find_element(By.ID, element).send_keys(value)

WebDriverWait(driver, 10).until(lambda driver: driver.find_element(By.ID, "username").get_attribute("username") != elements["username"])

for index, element in enumerate(elements):
    elements[element] = driver.find_element(By.ID, element).get_attribute("value")

with open("RPA_write.txt", 'w', encoding="utf-8") as file:
    file.write(pformat(elements))

driver.close()

++还有特殊的元件需要学习。++

相关推荐
utmhikari40 分钟前
【日常随笔】万字长文,如何用pyside6开发一个python桌面工具
前端·python·pyqt
小杨4043 小时前
python入门系列十四(多进程)
人工智能·python·pycharm
用户277844910499318 小时前
借助DeepSeek智能生成测试用例:从提示词到Excel表格的全流程实践
人工智能·python
JavaEdge在掘金20 小时前
ssl.SSLCertVerificationError报错解决方案
python
我不会编程55521 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
老歌老听老掉牙21 小时前
平面旋转与交线投影夹角计算
python·线性代数·平面·sympy
满怀101521 小时前
Python入门(7):模块
python
无名之逆21 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
你觉得20521 小时前
哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义下载方法
大数据·人工智能·python·gpt·学习·机器学习·aigc
啊喜拔牙21 小时前
1. hadoop 集群的常用命令
java·大数据·开发语言·python·scala