python - Python selenium:无法定位元素(//input[@type='file']')
问题描述
我正在尝试使用 python 自动化上传文件。当我尝试执行下面的代码时,python selenium 会引发错误。即使我尝试等待 10 秒以避免同步问题。
driver.execute_script('window.open("https://ocr.space/" , "new window")')
Imagepath = r"C:\User\Desktop\banner.png"
field=driver.find_element_by_xpath('//input[@type="file"]')
field.send_keys(Imagepath)
NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//input[@type="file"]"}
网址:
HTML 片段:
<div class="span8">
<input type="file" id="imageFile" class="form-control choose valid">
</div>
解决方案
更改代码以启动 urlget
似乎可以解决问题。
from selenium import webdriver
driver = webdriver.Chrome("./chromedriver")
driver.get("https://ocr.space/")
image = r"C:\Users\Thanthu Nair\Desktop\soc360.png"
field=driver.find_element_by_xpath('//input[@type="file"]')
field.send_keys(image)
还要确保提供的路径C:\User\Desktop\banner.png
是正确的,否则你会得到另一个异常。这只是我的假设,该路径可能是错误的,因为通常桌面文件夹位于用户名文件夹内,用户名位于用户文件夹内。在这种情况下,根据您提供的路径,您的 Desktop 文件夹位于 User 文件夹中。
推荐阅读
- facebook - 克服广告素材类型 - PRIVACY_CHECK_FAIL 错误
- mysql - MySQL 事件,在 INSERT 中选择
- angular - 如何创建 Angular 内部 URL 超链接?
- php - 用 IF 同时插入两个表
- coldfusion - 如何捕获 ColdFusion CFHTTP 超时异常?
- c# - 我应该怎么做才能获得有效的测试结果(字符串 [])而不是 WhereEnumerableIterator
[] 用于使用 Linq 的方法? - javascript - 属性'不存在于类型'DishdetailComponent'
- raspbian - 启用它后,raspbian ssh 连接被拒绝
- spring - Spring Kafka 不尊重 max.poll.records 的奇怪行为
- powershell - 如何读取从 terraform 传递到 powershell userdata 脚本的 Map 类型变量的值?