selenium icon indicating copy to clipboard operation
selenium copied to clipboard

[🐛 Bug]: Get download file in client

Open yairddj2 opened this issue 3 years ago • 37 comments

What happened?

i have dynamic grid env , was working with old implantation for support download ( - SE_OPTS=--downloads-path /home/seluser/Downloads ,etc) and it worked fine . now started to config new download way ( - SE_OPTS=--enable-managed-downloads true)

https://www.selenium.dev/documentation/grid/configuration/cli_options/#enabling-managed-downloads-by-the-node.

issue is after download file in browser during test, and run get for http://localhost:4444/session/my session id/se/files got an empty respond value like below .

{
  "value": {
    "names": [
    ]
  }
}

How can we reproduce the issue?

in node docker set environment : - SE_OPTS=--enable-managed-downloads true
in code set desired_capabilities=  "se:downloadsEnabled": "true"

Relevant log output

no error in log

Operating System

dynamic-grid docker

Selenium version

latest

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

latest

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

latest

Are you using Selenium Grid?

No response

yairddj2 avatar May 14 '23 14:05 yairddj2

@yairddj2, 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!

github-actions[bot] avatar May 14 '23 14:05 github-actions[bot]

Are you calling this before you quit your session?

titusfortner avatar May 14 '23 16:05 titusfortner

yes , only while session is active I get respond , if test/session is end then error is deferent (something like no session found )

yairddj2 avatar May 14 '23 17:05 yairddj2

Is it possible the file has not finished downloading?

titusfortner avatar May 14 '23 22:05 titusfortner

No this is not the case file is download and only then i tried to get file info , did develop team test "enable-managed-downloads true" in dynamic grid , if yes can you share the setup ? maybe something is missing in mine.

yairddj2 avatar May 15 '23 06:05 yairddj2

We missed Dynamic Grid when we revamped the downloads feature. I just fixed it so it works out of the box in 4.10.0.

In the mean time, you can add manually the "se:downloadsEnabled": "true" capability to the stereotypes in the config.toml.

diemol avatar May 15 '23 11:05 diemol

    "selenium/standalone-firefox:4.9.1-20230508", '{"browserName": "firefox", "platformName": "linux", "se:downloadsEnabled": true}',
    "selenium/standalone-chrome:4.9.1-20230508", '{"browserName": "chrome", "platformName": "linux", "se:downloadsEnabled": true}',
    "selenium/standalone-edge:4.9.1-20230508", '{"browserName": "MicrosoftEdge", "platformName": "linux", "se:downloadsEnabled": true}'

diemol avatar May 15 '23 11:05 diemol

@diemol even after set as you suggested got the same issue of 200 respond code without values

yairddj2 avatar May 15 '23 13:05 yairddj2

This is what I used:

[docker]
# Configs have a mapping between the Docker image to use and the capabilities that need to be matched to
# start a container with the given image.
configs = [
    "selenium/standalone-firefox:4.9.1-20230508", '{"browserName": "firefox", "platformName": "linux", "se:downloadsEnabled": true}',
    "selenium/standalone-chrome:4.9.1-20230508", '{"browserName": "chrome", "platformName": "linux", "se:downloadsEnabled": true}',
    "selenium/standalone-edge:4.9.1-20230508", '{"browserName": "MicrosoftEdge", "platformName": "linux", "se:downloadsEnabled": true}'
    ]

# URL for connecting to the docker daemon
# host.docker.internal works for macOS and Windows.
# Linux could use --net=host in the `docker run` instruction or 172.17.0.1 in the URI below.
# To have Docker listening through tcp on macOS, install socat and run the following command
# socat -4 TCP-LISTEN:2375,fork UNIX-CONNECT:/var/run/docker.sock
url = "http://127.0.0.1:2375"
# Docker image used for video recording
video-image = "selenium/video:ffmpeg-4.3.1-20230508"

# Uncomment the following section if you are running the node on a separate VM
# Fill out the placeholders with appropriate values
#[server]
#host = <ip-from-node-machine>
#port = <port-from-node-machine>

docker compose

# To execute this docker-compose yml file use `docker-compose -f docker-compose-v3-dynamic-grid.yml up`
# Add the `-d` flag at the end for detached execution
# To stop the execution, hit Ctrl+C, and then `docker-compose -f docker-compose-v3-dynamic-grid.yml down`
version: "3"
services:
  node-docker:
    image: selenium/node-docker:4.9.1-20230508
    volumes:
      - ./assets:/opt/selenium/assets
      - ./NodeDocker/config.toml:/opt/bin/config.toml
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
      - SE_OPTS=--enable-managed-downloads true

  selenium-hub:
    image: selenium/hub:4.9.1-20230508
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

Test

	void canListDownloadedFiles() throws InterruptedException, MalformedURLException {
		URL gridUrl = new URL(remoteUrl);
		ChromeOptions options = new ChromeOptions();
		options.setCapability("se:downloadsEnabled", true);
		RemoteWebDriver driver = new RemoteWebDriver(gridUrl, options);
		driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");
		driver.findElement(By.id("file-1")).click();
		driver.findElement(By.id("file-2")).click();
		SessionId sessionId = driver.getSessionId();

		// Waiting for the file to be remotely downloaded
		TimeUnit.SECONDS.sleep(3);

		HttpRequest request = new HttpRequest(GET, String.format("/session/%s/se/files", sessionId));
		try (HttpClient client = HttpClient.Factory.createDefault().createClient(gridUrl)) {
			HttpResponse response = client.execute(request);
			Map<String, Object> jsonResponse = new Json().toType(string(response), Json.MAP_TYPE);
			@SuppressWarnings("unchecked")
			Map<String, Object> value = (Map<String, Object>) jsonResponse.get("value");
			@SuppressWarnings("unchecked")
			List<String> names = (List<String>) value.get("names");
			assertTrue(names.contains("file_1.txt"));
			assertTrue(names.contains("file_2.jpg"));
		} finally {
			driver.quit();
		}
	}

diemol avatar May 15 '23 13:05 diemol

@diemol 1)When 4.10.0 planed to be published ? 2)I set the same config as you shared but still empty respond when curl get to files list , the deferent between us is that I download the file via the vnc (manual on the browser and not via code) , also the get request for files list is like below and not via code as you did curl -X GET "http://localhost:4444/session/90c0149a-2e75-424d-857a-e78734943d4c/se/files" is that makes any deferent ?

yairddj2 avatar May 18 '23 11:05 yairddj2

  1. End of May.
  2. The download needs to be done through the session, whatever you do manually is separate, Selenium cannot notice any manual interactions.

diemol avatar May 18 '23 12:05 diemol

Isn't the driver setting the download location for the browser in general? Shouldn't that be the same whether the download was initiated by Selenium or manual on that browser? Pretty sure as long as the browser was started by selenium, any download should populate that directory, and downloaded files should be available via the endpoint...

titusfortner avatar May 18 '23 12:05 titusfortner

Good point

diemol avatar May 18 '23 12:05 diemol

but the session ID obviously has to match the browser that was used to download, and the endpoint does nothing to ensure files have "finished downloading."

titusfortner avatar May 18 '23 12:05 titusfortner

Even after download the file from the test code files list is empty , file download location in endpoint is "/home/seluser/Downloads" is that ok? Can you guys think on other config that missing ?

yairddj2 avatar May 18 '23 12:05 yairddj2

Seems you are opening the browser manually, is that right?

diemol avatar May 18 '23 12:05 diemol

no , all by the code .

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from time import sleep

driver = webdriver.Remote( command_executor="http://xxxxx:4444/wd/hub", desired_capabilities={
"browserName": "chrome",
"platformName": "linux", "se:recordVideo": "True", "se:downloadsEnabled": "True"
})

driver.get("https://www.selenium.dev/selenium/web/downloads/download.html")

element = driver.find_element(By.ID, 'file-1') element.click() session = driver.session_id print (session)

sleep(300)

driver.quit()

yairddj2 avatar May 18 '23 13:05 yairddj2

Selenium generates a unique directory for downloads for each session, so the easiest way to access the files is with the endpoint

titusfortner avatar May 18 '23 13:05 titusfortner

maybe this is the issue , files are download to "/home/seluser/Downloads" and not to unique Selenium directory ?

yairddj2 avatar May 18 '23 14:05 yairddj2

You need to enable downloads when you start the grid, and you need to enable it in the capabilities when you start the browser. It should all be in the docs you linked at the top.

titusfortner avatar May 18 '23 16:05 titusfortner