首页 > 解决方案 > 无法使用 selenium 和 Python 将 PDF 从计算机上传到网络

问题描述

我编写此代码以将 PDF 从计算机上传到网站-

uploadPdf = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="lazyAttachCV"]/div/div/div[2]/div/div/div[1]/div[1]/section')))
uploadPdf.click()

使用上面的代码行,我能够打开弹出 的计算机窗口的上传弹出窗口 - 示例

然后我再次编辑代码以从 G 驱动器获取 pdf 文件,代码是

uploadPdf = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="lazyAttachCV"]/div/div/div[2]/div/div/div[1]/div[1]/section'))).send_keys("G://Python Project/Gabi.pdf")
uploadPdf.click()

我尝试使用上面的代码,从 G 驱动器中选择 pdf,但我以回溯错误结束

Traceback (most recent call last):
  File "G:\Python Project\Naukri Autamation\main.py", line 39, in <module>
    uploadResume = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="lazyAttachCV"]/div/div/div[2]/div/div/div[1]/div[1]/section'))).send_keys("G://Python Project/Gabi.pdf")
  File "G:\Python Project\Naukri Autamation\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT,
  File "G:\Python Project\Naukri Autamation\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "G:\Python Project\Naukri Autamation\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "G:\Python Project\Naukri Autamation\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=88.0.4324.104)

我不知道如何编码将pdf从计算机上传到网络,请帮助

标签: pythonseleniumselenium-webdriverxpathselenium-chromedriver

解决方案


您不能使用 selenium 与 windows pop 交互而是找到输入标记元素并将文件路径发送到该元素

例如

driver.find_element_by_tag_name("input").send_keys("c://a.pdf")

推荐阅读