ashbythorpe / selenider

Concise, Lazy and Reliable Wrapper for 'chromote' and 'selenium'
https://ashbythorpe.github.io/selenider/
Other
32 stars 2 forks source link

http POST? #26

Closed r2evans closed 2 months ago

r2evans commented 3 months ago

I'm testing selenider to attempt to upload content to a (non-public) website. I think I have managed to authenticate initially (have been unable using rvest). I think selenider is using the installed chromote package, not sure how to verify. (I don't have RSelenium installed.) sess$driver indicates <ChromoteSession>.

Once authenticated, how can I send something in an http POST connection?

The manual way to do what I'm trying to automate:

When I click on either upload option, in the browser devtools console the POST request is perfectly clear/structured, and has all of the components that were set with the pulldowns. I believe that since I know the ultimate URL and payload of the post request, I should be able to finish the authentication step and immediately upload files without stepping through "set pulldown, wait, set next, ..." cycle.

Is this possible?

jonovik commented 2 months ago

Perhaps you could do this using library(httr2)? Have a look at the introduction at https://httr2.r-lib.org where POST is mentioned at https://httr2.r-lib.org/#cb3.

What you'd try would be something along the lines of

httr2::request("https://your.server.example/api/create_test_item") |>
    # See also other req_auth_* functions
    httr2::req_auth_bearer_token("YOUR_API_TOKEN") |>
    # Adding a body turns the request into a POST; see other req_body_* functions
    httr2::req_body_form(title = "A test title") |>
    httr2::req_perform() |>
    httr2::resp_body_json() -> response
response  # Inspect the structure of the returned record

This assumes that you know the authentication data for one of the reqauth* functions, and that you know the names and suitable values for the body fields (title in the example above) from e.g. inspecting the form web page.

In my workflow to automate interaction with web pages, I often end up alternating between selenider, the Chrome inspector, and using plain rvest or httr2.

r2evans commented 2 months ago

My question was under-stated: I was asking if there was a way to initiate a POST request within the selenider framework. Similar to rvest::read_html_live (and chromote), there is no way to send a POST request from within the browser instance. I have to use live-html since the auth page (using ADFS and WS-Fed) requires javascript.

Ultimately, I found the distinct combination (and order, odd!) of cookies from chromote and headers that enabled me to make the POST request with httr::POST. Thanks for your reply.