我正在尝试使用 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"]"}
网站网址:
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/