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

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

                

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

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

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

        

""""""
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)

我们来个实战演练

        

""""""
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

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

                

""""""

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中 无法直接选中,我们还是要通过控制鼠标控制点击

代码如下

​
""""""
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标签时期控件

                

                

""""""

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标签

        

""""""

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.对滑动的时间控件操作

        

""""""

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()

        

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值