Skip to content

Instantly share code, notes, and snippets.

@beveradb
Created September 27, 2019 16:54
Show Gist options
  • Save beveradb/6ad7ead15ffe6b8fa8766852c7edbce5 to your computer and use it in GitHub Desktop.
Save beveradb/6ad7ead15ffe6b8fa8766852c7edbce5 to your computer and use it in GitHub Desktop.
Demonstrate controlling Selenium directly via HTTP requests made with curl to a Selenium Grid Hub
# Set hostname and port of Selenium Grid Hub:
GRID_HOST="localhost"
GRID_PORT=4444
# Create new session, by specifying capabilities (in this case, just Chrome) to get a connection to a browser on a selenium node
# Store this session ID in a variable so we can use it for the other commands
SESSION_ID=`curl -X POST http://$GRID_HOST:$GRID_PORT/wd/hub/session -d '{"desiredCapabilities":{"browserName":"chrome"}}'| grep '"sessionId"' | sed 's/.*sessionId": "\(.*\)".*/\1/g'`
# Tell the browser to load the URL for LMGTFY
curl -X POST http://$GRID_HOST:$GRID_PORT/wd/hub/session/$SESSION_ID/url -d '{"url":"https://lmgtfy.com"}'
# Find the search box on the page by selecting it by it's ID CSS selector
# Store this element ID in a variable, and use this to enter text in the next command
BOX_ID=`curl -X POST http://$GRID_HOST:$GRID_PORT/wd/hub/session/$SESSION_ID/element -d '{"using":"css selector","value":"#search_form_input_homepage"}' | sed 's/.*":"\(.*\)".*/\1/g'`
curl -X POST http://$GRID_HOST:$GRID_PORT/wd/hub/session/$SESSION_ID/element/$BOX_ID/value -d '{"text":"webdriver protocol"}'
# Find the search button on the page by selecting it by it's ID CSS selector
# Store this element ID in a variable, and use this to click it in the next command
BUTTON_ID=`curl -X POST http://$GRID_HOST:$GRID_PORT/wd/hub/session/$SESSION_ID/element -d '{"using":"css selector","value":"#search_link"}' | sed 's/.*":"\(.*\)".*/\1/g'`
curl -X POST http://$GRID_HOST:$GRID_PORT/wd/hub/session/$SESSION_ID/element/$BUTTON_ID/click -d '{}'
# Close this session now we no longer need it, so this browser is free for the next job
curl -X DELETE http://$GRID_HOST:$GRID_PORT/wd/hub/session/$SESSION_ID
@MatthewLymer
Copy link

For folks visiting and having issues with this script being able to create session on a Selenium 4 grid, I believe the create-session payload has changed.

The payload I've been using is:

{"capabilities":{"firstMatch":[{"platformName":"ANY"}]}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment