我有一个脚本,可以使用send_键将文件加载到浏览器中,这很好,但是我想在找不到文件时处理一个异常,但无法找到执行此操作的方法,我尝试捕获InvalidArgumentException和IOError,但它们似乎都无法捕获异常
chooseFile = driver.find_element_by_class_name("GDTLPL2DNW");
try:
chooseFile.send_keys("C:/temp/File.xml")
except IOError as e:
print ("Unable to Locate xml file to import, please check source directory")
exit(
else:
pass
DevTools listening on ws://127.0.0.1:56828/devtools/browser/0ea61685-38fb-4b23-9
92b-f16031cffa1a
Traceback (most recent call last):
File "C:\temp\FileLoadv3.py", line 132, in <module>
chooseFile.send_keys("C:/temp/File.xml")
File "C:\Users\hkijd\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "C:\Users\hkijd\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\hkijd\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\hkijd\AppData\Local\Programs\Python\Python38-32\lib\site-pack
ages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument:
File not found : C:/temp/File.xml
(Session info: chrome=85.0.4183.102)
我找到了更好的方法。在启动任何其他文件之前,只需使用
os.path.isfile
检查该文件是否存在相关问题 更多 >
编程相关推荐