python - Python Selenium 按类名将文件上传到网站
问题描述
我正在尝试使用 Python 将文件上传到网站。但是当我查看Developer Inspection Tool 时,没有id,也没有name。我正在尝试使用类名,但它不起作用。
来自 Selenium 的检查窗口:
从网站检查窗口:
我试过这样的方法:
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。
解决方案
如果您想要一个快速的解决方案,您可以随时尝试通过 ccs selctor 获取。右键单击检查寡妇中的元素。单击复制>选择器。然后在python中写driver.find_element_by_css_selector('paste the selector here')
这不是一个优雅的解决方案,但它应该可以工作。
推荐阅读
- r - 带字符串的 ifelse 语句
- kotlin - BleGattCharacteristicException:尝试指示->写入->通知 BLE 血糖仪时出现 GATT 异常状态 129
- python - 应用 anti_join 后我需要帮助找出代码错误
- python - 如何在 PyCharm 中更改 PEP 8 的命名约定检查?
- configure - 如何在 Cisco AnyConnect 安装程序中修改二进制文件?
- networkx - networkx 如何遍历图中的每个集群
- python - 选定列上的 DataFrame.values
- javascript - vuetify 表页面不起作用并重置回 1
- flutter - 有什么方法可以从 Flutter 中的 DocumentSnapshot 获取 DocumentReference?
- java - 有没有另一种方法可以在 android 日历选择器中添加天数