Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to download files with Selenider #30

Open
rgmerck opened this issue Oct 7, 2024 · 6 comments
Open

How to download files with Selenider #30

rgmerck opened this issue Oct 7, 2024 · 6 comments

Comments

@rgmerck
Copy link

rgmerck commented Oct 7, 2024

HI, I have a question.
In the process of webscrapping I need chromote to download files when clicking a css element on screen. The click works, but the default chromote session opened by selenider does not support downloading files. In chromote it is possible to set such an option with :

b$Browser$setDownloadBehavior(behavior = "allow", downloadPath = "<path_to_folder>")

But there does not seem to be a way to pass such settings through the chromote_options() function. Is this something that can be added, or is there a different way to achieve this in selenider?

@ashbythorpe
Copy link
Owner

If your selenider session is named session, then session$driver should get you the underlying chromote session, i.e.

session <- selenider_session()

session$driver$Browser$setDownloadBehavior(behavior = "allow", downloadPath = "<path_to_folder>")

@rgmerck
Copy link
Author

rgmerck commented Oct 8, 2024

thanks let me see if this works!

@rgmerck
Copy link
Author

rgmerck commented Oct 8, 2024

thanks! working great. I wonder if this would be worth adding to your documentation, and not just for the downloading part, but for passing options to chromote?

@ashbythorpe
Copy link
Owner

I'm working on a new vignette that would go through this (#34).

@beethoven111
Copy link

Unfortunately, it does not work for me with this website: "https://www-genesis.destatis.de/datenbank/online/statistic/43331/table/43331-0001#modal=table-download&params=JTVCJTdCJTIycGFyYW1zJTIyJTNBJTdCJTIyaWQlMjIlM0ElMjIlMjIlN0QlN0QlNUQ="

-> I want to download the .xlsx file by clicking the XLSX button.

My code:

session <- selenider_session()
session$driver$Browser$setDownloadBehavior(behavior = "allow", downloadPath = "C:/Users/a.name/downloads/data.xlsx")
open_url(session = session,"https://www-genesis.destatis.de/datenbank/online/statistic/47414/table/47414-0005#modal=table-download&params=JTVCJTdCJTIycGFyYW1zJTIyJTNBJTdCJTIyaWQlMjIlM0ElMjIlMjIlN0QlN0QlNUQ=")
button <- s(".button-button.statistic-table-download-modal-overlay-button[title='XLSX Format']") 
button |> elem_click()

The only thing that happens, is that an empty folder named "data.xlsx" will be saved in "C:/Users/a.name/downloads/".

Is there any solution to this? I would be really interested if anyone is able to download this file with selenider in R (chromote). The website was just recently launched by the German federal Statistical Office. I would be very glad for help. Thank you!

@ashbythorpe
Copy link
Owner

This works for me:

library(selenider)

session <- selenider_session(timeout = 10)

session$driver$Browser$setDownloadBehavior(behavior = "allow", downloadPath = ".test")

open_url("https://www-genesis.destatis.de/datenbank/online/statistic/47414/table/47414-0005#modal=table-download&params=JTVCJTdCJTIycGFyYW1zJTIyJTNBJTdCJTIyaWQlMjIlM0ElMjIlMjIlN0QlN0QlNUQ=")

s(".button-button.statistic-table-download-modal-overlay-button[title='XLSX Format']") |>
  elem_click()

# Wait for the file to be downloaded
Sys.sleep(10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants