首页 > 解决方案 > Python Selenium 按类名将文件上传到网站

问题描述

我正在尝试使用 Python 将文件上传到网站。但是当我查看Developer Inspection Tool 时,没有id,也没有name。我正在尝试使用类名,但它不起作用。

来自 Selenium 的检查窗口:

截图 1

从网站检查窗口:

截图 2

我试过这样的方法:

upload = driver.find_element_by_class_name("icomoon icon-upload2 toolbar-button fileinput-button")
upload = driver.find_element_by_class_name("fileupload")
upload = driver.find_element_by_class_name("btnContainer").

都失败了。

the error message:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".btnContainer"}
  (Session info: chrome=91.0.4472.77)

然后我使用:

upload = driver.find_element_by_css_selector('body > div.fileupload')
upload.send_keys(r'c\user\xxx\upload\excel.xlsx')
#or
upload = driver.find_element_by_xpath("//div[@class='fileupload']")
upload.send_keys(r'c\user\xxx\upload\excel.xlsx')

错误信息:

ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=91.0.4472.77)

谁能帮我这个?

谢谢,

JH。

标签: pythonselenium-webdriver

解决方案


如果您想要一个快速的解决方案,您可以随时尝试通过 ccs selctor 获取。右键单击检查寡妇中的元素。单击复制>选择器。然后在python中写driver.find_element_by_css_selector('paste the selector here')

这不是一个优雅的解决方案,但它应该可以工作。


推荐阅读