Here is a quick tutorial on how you may add items to a shopping cart on eCommerce websites using ScrapingBee API via a JS scenario on Python.
1. You would need to identify any CSS selector that uniquely identifies the button or 'add to cart' element you wish to click. This can be done via the inspect element option on any browser, more details can be found on this tutorial:
https://www.scrapingbee.com/tutorials/how-to-extract-css-selectors-using-chrome/
2. Once you successfully identify the button ID, you can add it to your JS scenario via the "click" parameter and that it follows the request to return a response from the cart page:
{
"instructions": [
{"wait": 4000},
{ "click": "#cart-button-id" },
{"wait": 2000},
{"evaluate": "window.location.href='cart_page_url'"},
{"wait": 5000}
]
}
In this case, the evaluate function would take you to the cart page and return a response from there. We can try it out for this specific Amazon URL:
https://www.amazon.com/ASUS-ROG-Strix-Gaming-Laptop/dp/B0DZZWMB2L
*Note that the specific URL we used may expire, simply swap it with the URL you wish to scrape.
Using the example URL, the request in Python would look like this:
import requests
def send_request():
response = requests.get(
url='https://app.scrapingbee.com/api/v1',
params={
'api_key': 'API_key',
'url': 'https://www.amazon.com/ASUS-ROG-Strix-Gaming-Laptop/dp/B0DZZWMB2L',
'js_scenario': '{"instructions":[{"wait_for_and_click":"#add-to-cart-button"},{"wait":2000},{"evaluate":"window.location.href=\'https://www.amazon.com/cart?ref_=ewc_gtc\'"},{"wait":5000}]}'},)
print('Response HTTP Status Code: ', response.status_code)
print('Response HTTP Response Body: ', response.content)
send_request()
Go back to tutorials