0

I am having a hard time in this problem: I have to perform some tasks with requests (it's mandatory that I use requests for these tasks, so I cannot use all the way selenium in the first place) and then "transfer" the session that I have been carried on (with requests.session() ) to selenium and opening a webdriver with that session loaded.

How do I do that?

Here's the code:

import requests
import time
from selenium import webdriver

delay = 2
headers = {
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36'
}

session = requests.Session()
session.headers.update(headers)


driver = webdriver.Chrome()
driver.get('https://www.nigramercato.com')

payloadcartupdate = {
  'token': '1eab12d3844d5201e62ff74588607f52',
  'id_product': '5089',
  'id_customization': '0',
  'group[1]': '28',
  'qty': '1',
  'add': '1',
  'action': 'update'
}

payloadajax = {
  'id_product_attribute': '29465',
  'id_product': '5039',
  'action': 'add-to-cart'
}

payloadorder = {
  'ajax_request': '1',
  'action': 'getShippingAndPaymentBlocks',
  'token': '1eab12d3844d5201e62ff74588607f52'
}

payloaddelivery = {
  'delivery_option[0]': '48',
  'selectDeliveryOption': '',
  'ajax_request': '1',
  'action': 'selectDeliveryOption',
  'token': '1eab12d3844d5201e62ff74588607f52'
}

payloadpayment = {
  'optionId': 'payment-option-2',
  'payment_fee': '0',
  'ajax_request': '1',
  'action': 'selectPaymentOption',
  'token': '1eab12d3844d5201e62ff74588607f52'
}

def visit_page():
  print('Visiting the page...')
  visitpage = session.get('https://nigramercato.com/en/jordan/5039-29465-AIR-JORDAN-1-RETRO-HIGH-OG-555088-060.html')
  print(f"Successfully visited the page, waiting the delay ({delay} seconds)...")
  print(f"Status code of the operation: {visitpage.status_code}")



def ajax_add_to_cart():
  print('Adding to cart...')
  ajaxaddtocart = session.post('https://nigramercato.com/en/module/ps_shoppingcart/ajax', data=payloadajax)
  print(f'Successfully added to cart, waiting the delay ({delay} seconds)...')
  print(f'Status code of the operation: {ajaxaddtocart.status_code}')
  time.sleep(delay)

def update_cart():
  print('Updating cart...')
  updatecart = session.post('https://nigramercato.com/en/cart', data=payloadcartupdate)
  print(f'Successfully updated cart, waiting the delay ({delay} seconds)...')
  time.sleep(delay)

def final_step():
  driver.get('https://nigramercato.com/en/order')
  cookiecount = 0
  for c in session.cookies():
    driver.add_cookie({
      'name': c.name,
      'value': c.value
    })
    cookiecount += 1
    print(f"Successfully imported {cookiecount} cookies")




visit_page()
ajax_add_to_cart()
update_cart()
final_step()

Here's the error I get:

Traceback (most recent call last):
  File ".\nigramercato.py", line 129, in <module>
    final_step()
  File ".\nigramercato.py", line 112, in final_step
    for c in session.cookies():
TypeError: 'RequestsCookieJar' object is not callable
Miopadrone
  • 31
  • 5

1 Answers1

0

python-requests cookies export to selenium

The error for c in session.cookies(): is an error because session.cookies is an attribute. () calls the function, but it's not a function. The original link also did not have (). In conclusion, it should be: for c in session.cookies:.

Stuart
  • 465
  • 3
  • 6
  • Actually I am trying quite to do the exact opposite of what you proposed: I am trying to transfer a session already carried with requests TO selenium. Not from it. – Miopadrone Mar 13 '20 at 07:11
  • @Miopadrone okay. I modified the link above to a link that should do that. – Stuart Mar 13 '20 at 15:06
  • Again, unfortunately the solution seems not working. Maybe I am doing something wrong but that's what I get `TypeError: 'RequestsCookieJar' object is not callable` – Miopadrone Mar 14 '20 at 08:54
  • @Miopadrone you're going to have to provide the code for what you tried and what gives you that error. – Stuart Mar 14 '20 at 14:05
  • yeah, for sure. Here it is. I am trying to automate the process of buying something on a site. – Miopadrone Mar 15 '20 at 01:40
  • there are some payloads that I don't use in the code because I cut out some parts of it – Miopadrone Mar 15 '20 at 01:46
  • The error log is missing from your update to the question. [How to ask a good question](https://stackoverflow.com/help/how-to-ask) is good to read as well as [how to create a minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – Stuart Mar 15 '20 at 01:51
  • Hi, I am sorry for the confusion. – Miopadrone Mar 16 '20 at 03:48
  • Again, I feel you are suggesting a way to do something with selenium AND then using requests. I want to do the opposite. – Miopadrone Mar 18 '20 at 20:49
  • The goal I want to accomplish is the manual checkout; since the payment provider for that site is Paypal, which is obviously very safe, I won't be able to inject a session and let Paypal process the payment without letting them know I am actually a bot (and block me) – Miopadrone Mar 18 '20 at 20:51
  • If you don't want to use requests then why are you asking about it? you don't need requests at all if you're using selenium. – Stuart Mar 18 '20 at 20:58
  • Because requests it's a lot faster, and the handling error is way better with requests. – Miopadrone Mar 19 '20 at 03:15
  • That's why I have to use requests. And just for clarification, I want to use requests. It's almost mandatory for me to use it. I just want to be able to transfer the session I used for 90% of the code from requests to selenium. I know it may be sound confusing, does that make sense? – Miopadrone Mar 19 '20 at 03:17
  • Not really. In my experience, transferring the cookies is sufficient in order to get what you want from requests. – Stuart Mar 19 '20 at 05:53
  • your answer clearly suggests how to take the cookies generated with selenium and transfer the cookies in the requests session. I want to do the opposite. – Miopadrone Mar 20 '20 at 03:05
  • What exactly is it that requests gives you that is not obtained by using selenium? Using requests and transferring cookies to selenium is possible (just search). But, I don't see how that will benefit you. Most of the time you need javascript enabled (i.e. use selenium) to get the cookies set. Requests is essentially like turning off javascript in your browser and visiting the site--it won't set cookies on many sites. – Stuart Mar 20 '20 at 03:53
  • I have written in the post that I specifically have to use requests, and I don't see how is useful to explain why. However, I have to use requests jut because what I am trying to do is buy a product extremely limited and that only a few people will be able to get. I have to use requests because I will combine it with beautifulsoup and I will open Selenium only when the item is in the cart. I won't do the whole thing with selenium because I will be running hundreds of tasks. All the pc will have a hard time if you open 500 selenium instances. – Miopadrone Mar 20 '20 at 05:52
  • And, by the way, I have all the cookies I need because if I try to export the cookies and import them with EditThisCookie in a chrome tab, it works. So it doesn't matter if javascript is not enabled – Miopadrone Mar 20 '20 at 05:54
  • Stuarts, are you still there? Could you help me please? – Miopadrone Mar 24 '20 at 06:02