Reputation: 31
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
Upvotes: 0
Views: 971
Reputation: 474
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:
.
Upvotes: 0