python web自动话(⽂件上传和⽇期控件)

1.⽂件上传操作-input标签⽂件选择

我们有如下的文件上传的联系网站,我们可以定位到选择文件,但是点击选择文件无法定位到

我们可以看到这个选择文件的标签是input

我们直接使用send_keys进行图片上传

python 复制代码
""""""
import time

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get(r"E:\Python\pythonProjects\Webtest0524\day7\01 文件上传操作\文件上传.html")

# 定位文件上传的按钮元素
el = driver.find_element(By.ID, "fileToUpload")
time.sleep(3)

# 上传文件
el.send_keys(r"E:\Python\pythonProjects\Webtest0524\day7\01 文件上传操作\上传文件.jpeg")

time.sleep(3)

我们来个实战演练

python 复制代码
""""""
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

"""
    1.打开项目
    2.完成登录
    3.跳转到'作家专区'
    4.点击'发布作品'
    5.完成表单的填写(包含文件上传),并点击提交
    6.手动去验证是否发布作品成功
"""

# 前置准备
driver = webdriver.Chrome()
username = "18166595233"
password = "123456"
wait = WebDriverWait(driver,5)


# 1.打开项目
driver.get("http://120.25.127.201:18001/user/login.html")
locator=(By.XPATH,"//h3")
wait.until(ec.text_to_be_present_in_element(locator,"登陆读书屋"))

# 2.完成登录
driver.find_element(By.ID,"txtUName").send_keys(username)
driver.find_element(By.ID,"txtPassword").send_keys(password)
driver.find_element(By.ID,"btnLogin").click()
# 3.跳转到'作家专区'
driver.find_element(By.CSS_SELECTOR,"#navModule > li:nth-child(5) > a").click()

# 4.点击'发布作品'
# 切换窗口
all_handel=driver.window_handles
driver.switch_to.window(all_handel[-1])

# 点击'发布小说'

driver.find_element(By.LINK_TEXT,"发布小说").click()
# 5.完成表单的填写(包含文件上传),并点击提交
select_element=driver.find_element(By.ID,"catId")
select_object=Select(select_element)
select_object.select_by_value("2")

driver.find_element(By.ID,"bookName").send_keys("明日方舟启动")


# 上传文件
file0=driver.find_element(By.ID,"file0")
file0.send_keys("E:\Python\资料\hutao.png")

# 小说介绍:
driver.find_element(By.ID,"bookDesc").send_keys("这时一本关于主人公和阿米娅的泰拉大陆冒险之旅")
time.sleep(5)
# 提交
driver.find_element(By.ID,"btnRegister").click()

2.⽂件上传操作-⾮input标签⽂件选择

⾮input标签的⽂件上传, selenium是⽆法处理的,使⽤PyAutoGui进⾏处理

安装PyAutoGui:

pip install pyautogui

然后我们选择图片在要获取鼠标的坐标,把鼠标移动到文件的位置

python 复制代码
""""""

import time

import pyautogui  # pip install pyautogui -i https://mirrors.aliyun.com/pypi/simple/
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get(r"E:\Python\pythonProjects\Webtest0524\day7\01 文件上传操作\非input文件上传.html")

# 定位文件上传的按钮元素
el = driver.find_element(By.CLASS_NAME, "custom-upload-button")

# 点击上传文件的按钮
# el.click()

# 如果使用click报错,就用ActionChains操作
ActionChains(driver).click(el).perform()

# 获取鼠标的坐标(把鼠标移动到文件的位置)
x, y = pyautogui.position()
print(f"鼠标的当前坐标 -- x: {x}, y: {y}")

# 点击"文稿"
pyautogui.moveTo(1794, 451)
time.sleep(2)
pyautogui.click()

# 点击'图片'
pyautogui.moveTo(2462, 381)
time.sleep(2)
pyautogui.click()

pyautogui.moveTo(2479, 680)
pyautogui.click()

# 选择上传的文件
pyautogui.moveTo(2204, 509)
pyautogui.click()

# 点击"打开"
pyautogui.moveTo(2479, 680)
pyautogui.click()

"""
如果是windows系统,要比mac系统简单的多
"""

# 获取鼠标的坐标(把鼠标移动到文件的位置)
x, y = pyautogui.position()
print(f"鼠标的当前坐标 -- x: {x}, y: {y}")

# 移动到地址输入栏
pyautogui.moveTo(x, y)
pyautogui.click()

# 删除掉原来的路径
pyautogui.press("backspace")

# 输入新的文件地址路径
pyautogui.write("新的文件地址路径")

# 连续按下两次回车键
pyautogui.press("enter")
pyautogui.press("enter")

3.多文件上传

这种我们发现

input是嵌套在lable中 无法直接选中,我们还是要通过控制鼠标控制点击

代码如下

python 复制代码
​
""""""
import time

import pyautogui
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get(r"E:\Python\pythonProjects\Webtest0524\day7\01 文件上传操作\多文件上传.html")

# 定位文件上传的按钮元素
el = driver.find_element(By.CLASS_NAME, "custom-upload-button")

# 点击上传文件的按钮
# el.click()

# 如果使用click报错,就用ActionChains操作
ActionChains(driver).click(el).perform()

# 获取鼠标的坐标(把鼠标移动到文件的位置)
x, y = pyautogui.position()
print(f"鼠标的当前坐标 -- x: {x}, y: {y}")

# 点击"文稿"
pyautogui.moveTo(284,54)
time.sleep(2)
pyautogui.click()
pyautogui.press( "backspace")
pyautogui.write("E:\Python")
pyautogui.press("enter")
pyautogui.press("enter")
#
# 按下ctrl键不动
pyautogui.keyDown("ctrl")

pyautogui.moveTo(323, 358)
time.sleep(2)
pyautogui.click()

pyautogui.moveTo(307, 379)
time.sleep(2)
pyautogui.click()

# # 松开ctrl键
pyautogui.keyUp("command")

# # 点击"打开"
pyautogui.moveTo(642, 451)
pyautogui.click()



​

执行如下

4.对⽇期控件的处理

我们在自动化中会遇到时间控件,我们怎么处理呢

1.input标签时期控件

python 复制代码
""""""

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get(r"E:\Python\pythonProjects\Webtest0524\day7\02 对日期控件的处理\layui仿写页面\日期控件.html")

# input标签:直接输入时间
driver.find_element(By.ID, "test1").send_keys("2024-5-5")

2.非input时期控件

我们看到这个日期控件不属于input标签

python 复制代码
""""""

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get(r"E:\Python\pythonProjects\Webtest0524\day7\02 对日期控件的处理\layui仿写页面\日期控件.html")

# 选择类型的日期控件:
# 先滑动到底部的日期控件
el = driver.find_element(By.ID, "layui-laydate35")
ActionChains(driver).scroll_to_element(el).perform()

# 对非input输入框进行点击,触发日期框架弹窗
driver.find_element(By.ID, "test28").click()

# 选择年份
driver.find_element(By.XPATH, '//div[@id="layui-laydate31"]/div[1]/div[1]/div/span[1]').click()  # 点击年份选择
driver.find_element(By.XPATH, '//*[@id="layui-laydate31"]/div[1]/div[2]/ul/li[15]').click()  # 2030年

# 选择月份
driver.find_element(By.XPATH, '//*[@id="layui-laydate31"]/div[1]/div[1]/div/span[2]').click()  # 点击月份选择
driver.find_element(By.XPATH, '//*[@id="layui-laydate31"]/div[1]/div[2]/ul/li[1]').click()  # 一月

# 选择日期
driver.find_element(By.XPATH, '//*[@id="layui-laydate31"]/div[1]/div[2]/table/tbody/tr[5]/td[5]').click()  # 31号

# 2030年1月31号

3.对滑动的时间控件操作

python 复制代码
""""""

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.actions.wheel_input import ScrollOrigin
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get(r"E:\Python\pythonProjects\Webtest0524\day7\02 对日期控件的处理\layui仿写页面\日期控件.html")

# 点击时间选择器,触发滑动选择器
driver.find_element(By.ID, "test4").click()

"""
方式一:直接滚动到元素
"""

# 选择'时'
# hours = driver.find_element(By.XPATH, '//*[@id="layui-laydate5"]/div[1]/div[2]/ul/li[1]/ol/li[23]')
# ActionChains(driver).scroll_to_element(hours).perform()
# hours.click()

# 选择'分'
# minutes = driver.find_element(By.XPATH, '//*[@id="layui-laydate5"]/div[1]/div[2]/ul/li[2]/ol/li[34]')
# ActionChains(driver).scroll_to_element(minutes).perform()
# minutes.click()

# 选择'秒'
# seconds = driver.find_element(By.XPATH, '//*[@id="layui-laydate5"]/div[1]/div[2]/ul/li[3]/ol/li[20]')
# ActionChains(driver).scroll_to_element(seconds).perform()
# seconds.click()

# 点击'确定'按钮
# driver.find_element(By.XPATH, '//*[@id="layui-laydate5"]/div[2]/div/span[3]').click()

"""
方式二:模拟滑动到元素出现
"""
# 选择'时'
hours = driver.find_element(By.XPATH, '//*[@id="layui-laydate5"]/div[1]/div[2]/ul/li[1]/ol/li[1]')
el = driver.find_element(By.XPATH, '//*[@id="layui-laydate5"]/div[1]/div[2]/ul/li[1]/ol/li[23]')

# 定位滑动的起点
sliding_start = ScrollOrigin.from_element(hours)

ActionChains(driver).click_and_hold(hours).scroll_from_origin(sliding_start, 0, 600).click(el).perform()
相关推荐
正小安18 分钟前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
吾爱星辰42 分钟前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
ChinaDragonDreamer42 分钟前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
FreakStudio43 分钟前
全网最适合入门的面向对象编程教程:56 Python字符串与序列化-正则表达式和re模块应用
python·单片机·嵌入式·面向对象·电子diy
IT良43 分钟前
c#增删改查 (数据操作的基础)
开发语言·c#
丶21361 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
Kalika0-02 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
_.Switch2 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光2 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   2 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发