Python Selenium : Unable to locate the element (//input[@type ='file' ]')

标签 python html selenium automation

我正在尝试使用 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: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type="file"]"}

网站网址:

https://ocr.space/

HTML 片段:

<div class="span8">
  <input type="file" id="imageFile" class="form-control choose valid">
</div>

最佳答案

更改代码以使用 get 启动 url 似乎可以解决该问题。

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 正确,否则您将收到另一个异常。这只是我的假设,这个路径可能是错误的,因为通常桌面文件夹位于用户名所在的文件夹内,而用户名位于用户文件夹内。在这种情况下,根据您提供的路径,您的“桌面”文件夹位于“用户”文件夹内。

关于Python Selenium : Unable to locate the element (//input[@type ='file' ]'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54958463/

相关文章:

python - python中使用for语句的无限循环

python - pandas 合并如何保持顺序?

Python 调度程序 vs 循环 + sleep

python - 使用 scrapy 在表中查找正确的数据

html - 为什么 "parallax"效果对未固定的背景图像不起作用

javascript - 隐藏包含移动状态、点火开关的表格行

css - Selenium Python 我正在尝试使用 XPATH 或 CSS 选择器从嵌套的 span 标记中选择一个下拉列表

java - 使用 Selenium Webdriver 在网页中移动鼠标

php - 我怎样才能摆脱我网站上的空白区域?

java - 如何在断言中将值写入字符串