我正在尝试使用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:消息:没有这样的元素:无法找到
元素:{“方法”:“ xpath”,“选择器”:“ //输入[@ 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
路径正确,否则会出现另一个异常。我只是以为此路径可能是错误的,因为通常Desktop文件夹位于用户名位于User文件夹内的文件夹内。在这种情况下,根据您提供的路径,您的桌面文件夹位于用户文件夹内。关于python - Python Selenium:无法找到元素(//input [@ type ='file']'),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54958463/