Загрузить файл с помощью Selenium с Python - PullRequest
0 голосов
/ 11 мая 2018

Я пытаюсь загрузить файлы на веб-сайт с помощью селена с помощью chromedriver. Все отлично работает когда я вручную ввожу путь к файлу: file_input.send_keys('C:\Scans\Test.pdf')

Проблема

Исключение выдается, когда я ввожу цикл цикла итерации списка путей к файлам.

for path in pathlist:
    if path.endswith('pdf'):
        file_input = driver.find_element_by_xpath("//input[@name='attacheFile']")
        file_input.send_keys(path)

Ошибка связана с частью кода send_keys.

File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
    'value': keys_to_typing(value)})
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: invalid argument: File not found : 
  (Session info: chrome=66.0.3359.117)
  (Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.16299 x86_64)

Что я пробовал

Расшифровка пути

for path in pathlist:
    if path.endswith('pdf'):
        modifpath = os.fsdecode(path)
        file_input = driver.find_element_by_xpath("//input[@name='attacheFile']")
        file_input.send_keys(modifpath)

Замена / на //

for path in pathlist:
    if path.endswith('pdf'):
        modifpath = os.fsdecode(path)
        modifpath = modifpath.replace("\\", '\\\\')
        file_input = driver.find_element_by_xpath("//input[@name='attacheFile']")
        file_input.send_keys(modifpath)

Использование инструмента os.path.abspath на декодированном пути

for path in pathlist:
    if path.endswith('pdf'):
        modifpath = os.fsdecode(path)
        file_input = driver.find_element_by_xpath("//input[@name='attacheFile']")
        file_input.send_keys(os.path.abspath(modifpath))

Использование инструмента os.path.abspath в исходном пути

for path in pathlist:
    if path.endswith('pdf'):
        file_input = driver.find_element_by_xpath("//input[@name='attacheFile']")
        file_input.send_keys(os.path.abspath(path))

У вас есть предложения? Я искал везде, но не мог найти ответ, кроме как использовать модуль AutoIt, который не является кроссплатформенной альтернативой.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...