Skip to content

[🐛 Bug]: Fail to download PDF or zip file from remote to client on Remote webdriver  #13956

@15975518086

Description

@15975518086

What happened?

error:

D:\Python\Python311\python.exe D:/OfflineaCare/ndb/program/test/test_oooooooo.py
Traceback (most recent call last):
File "D:\OfflineaCare\ndb\program\test\test_oooooooo.py", line 51, in
driver.download_file(downloadable_file, target_directory)
File "D:\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1155, in download_file
zip_ref.extractall(target_directory)
File "D:\Python\Python311\Lib\zipfile.py", line 1679, in extractall
self._extract_member(zipinfo, path, pwd)
File "D:\Python\Python311\Lib\zipfile.py", line 1734, in _extract_member
shutil.copyfileobj(source, target)
File "D:\Python\Python311\Lib\shutil.py", line 197, in copyfileobj
buf = fsrc_read(length)
^^^^^^^^^^^^^^^^^
File "D:\Python\Python311\Lib\zipfile.py", line 953, in read
data = self._read1(n)
^^^^^^^^^^^^^^
File "D:\Python\Python311\Lib\zipfile.py", line 1021, in _read1
data += self._read2(n - len(data))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Python\Python311\Lib\zipfile.py", line 1056, in _read2
raise EOFError
EOFError

Process finished with exit code 1

How can we reproduce the issue?

The code bellow is click the button,then download the .docx file(or zip or pdf)
code:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

options = webdriver.ChromeOptions()
options.enable_downloads = True
driver = webdriver.Remote(command_executor='http://192.168.3.35:4444/wd/hub', options=options)
driver.maximize_window()
driver.implicitly_wait(5)
driver.get("http://127.0.0.1:8000/login_page")
driver.find_element(By.XPATH,"//button[text()='导出']").click()
time.sleep(5)
file_names = driver.get_downloadable_files()
downloadable_file = file_names[0]
target_directory = r'D:\dtmp'
driver.download_file(downloadable_file, target_directory)
time.sleep(10)


node setting:
java -jar selenium-server-4.20.0.jar node --hub http://192.168.3.35:4444   --host 192.168.3.35 --port 5557  --enable-managed-downloads true



I found the the source code in webdriver.py the method :def get_downloadable_files, has some issues
if i set the name to be zip like 'file_name = 'package.zip' ,then i can run successfully, but without this ,it will fail


        contents = self.execute(Command.DOWNLOAD_FILE, {"name": file_name})["value"]["contents"]
        # file_name = 'package.zip'
        target_file = os.path.join(target_directory, file_name)
        with open(target_file, "wb") as file:
            file.write(base64.b64decode(contents))

        with zipfile.ZipFile(target_file, "r") as zip_ref:
            zip_ref.extractall(target_directory)

Relevant log output

D:\Python\Python311\python.exe D:/OfflineaCare/ndb/program/test/test_oooooooo.py
Traceback (most recent call last):
  File "D:\OfflineaCare\ndb\program\test\test_oooooooo.py", line 51, in <module>
    driver.download_file(downloadable_file, target_directory)
  File "D:\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1155, in download_file
    zip_ref.extractall(target_directory)
  File "D:\Python\Python311\Lib\zipfile.py", line 1679, in extractall
    self._extract_member(zipinfo, path, pwd)
  File "D:\Python\Python311\Lib\zipfile.py", line 1734, in _extract_member
    shutil.copyfileobj(source, target)
  File "D:\Python\Python311\Lib\shutil.py", line 197, in copyfileobj
    buf = fsrc_read(length)
          ^^^^^^^^^^^^^^^^^
  File "D:\Python\Python311\Lib\zipfile.py", line 953, in read
    data = self._read1(n)
           ^^^^^^^^^^^^^^
  File "D:\Python\Python311\Lib\zipfile.py", line 1021, in _read1
    data += self._read2(n - len(data))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\Python311\Lib\zipfile.py", line 1056, in _read2
    raise EOFError
EOFError

Process finished with exit code 1

Operating System

WINDOWS10

Selenium version

selenium 4.20.0 python 3.11.3

What are the browser(s) and version(s) where you see this issue?

Chrome 124

What are the browser driver(s) and version(s) where you see this issue?

124.0.6367.61

Are you using Selenium Grid?

selenium-server-4.20.0.jar

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-pyPython BindingsI-defectSomething is not working as intendedR-help wantedIssues looking for contributions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions