SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
29.77k stars 8.02k forks source link

[🐛 Bug]: Python download file from remote to local machine doesn't work, EOFError happends #14077

Closed wenwen12341234 closed 4 weeks ago

wenwen12341234 commented 4 weeks ago

What happened?

In Python script, I try to download a file around 20 MB from website to local machine using RemoteWebDriver, EOF error is happening while execute the step driver.download_file(downloadable_file, target_directory). Actually this download file has saved into temp folder meanwhile but error occurs in unzip, the following is source code where exception happens:

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

How can we reproduce the issue?

options = webdriver.ChromeOptions()
options.enable_downloads = True
url = "http://127.0.0.1:4444"
driver = webdriver.Remote(command_executor=url,  options=options)
driver.file_detector = LocalFileDetector()
driver.get("https://myjob.page/tools/test-files")
download_link = driver.find_element(By.XPATH, "/html/body/main/div/div/div[1]/a[5]")
download_link.click()
WebDriverWait(driver, 180).until(lambda d: "20MB.bin" in d.get_downloadable_files())
files = driver.get_downloadable_files()
file_names = ["20MB.bin"]
assert sorted(files) == sorted(file_names)
downloadable_file = file_names[0]
target_directory = tempfile.mkdtemp(prefix="python_download")
driver.download_file(downloadable_file, target_directory)

Relevant log output

C:\Python310\python.exe D:\selenium\upload&download\download.py 
Traceback (most recent call last):
  File "D:\selenium\upload&download\download.py", line 26, in <module>
    driver.download_file(downloadable_file, target_directory)
  File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1155, in download_file
    zip_ref.extractall(target_directory)
  File "C:\Python310\lib\zipfile.py", line 1645, in extractall
    self._extract_member(zipinfo, path, pwd)
  File "C:\Python310\lib\zipfile.py", line 1700, in _extract_member
    shutil.copyfileobj(source, target)
  File "C:\Python310\lib\shutil.py", line 195, in copyfileobj
    buf = fsrc_read(length)
  File "C:\Python310\lib\zipfile.py", line 925, in read
    data = self._read1(n)
  File "C:\Python310\lib\zipfile.py", line 993, in _read1
    data += self._read2(n - len(data))
  File "C:\Python310\lib\zipfile.py", line 1028, in _read2
    raise EOFError
EOFError

Process finished with exit code 1

Operating System

Windows11

Selenium version

4.17.0

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

Chrome

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

ChromeDriver

Are you using Selenium Grid?

NA

github-actions[bot] commented 4 weeks ago

@wenwen12341234, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

titusfortner commented 4 weeks ago

Duplicate #13956 should be fixed in next release, or you can try the nightly version to verify.