Why a JSON error in python selenium request? - python

I don't understand the following error I have using python selenium. Can you explain please this error?
def getdriver(url):
s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.minimize_window()
driver.get(url)
return(driver)
driver = getdriver("https://address.com")
driver.switch_to.frame('idIframeEngagement')
equipes = driver.find_elements(By.CLASS_NAME, 'menu')
for elt in equipes:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(((By.PARTIAL_LINK_TEXT, elt.text.encode('utf-8'))))).click()
I connect to a webpage. I get all the links I need and the I want to visit all of them using a loop but I get all this error:
Traceback (most recent call last):
File "C:\code\python\Avenir\resu\main.py", line 77, in <module>
getinfoFFBB()
File "C:\code\python\Avenir\resu\main.py", line 57, in getinfoFFBB
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(((By.PARTIAL_LINK_TEXT, elt)))).click()
File "C:\Users\elman\AppData\Local\Programs\Python\Python310-32\lib\site-packages\selenium\webdriver\support\wait.py", line 81, in until
value = method(self._driver)
File "C:\Users\elman\AppData\Local\Programs\Python\Python310-32\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 312, in _predicate
target = driver.find_element(*target) # grab element at locator
File "C:\Users\elman\AppData\Local\Programs\Python\Python310-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 855, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\elman\AppData\Local\Programs\Python\Python310-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 426, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Users\elman\AppData\Local\Programs\Python\Python310-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 342, in execute
data = utils.dump_json(params)
File "C:\Users\elman\AppData\Local\Programs\Python\Python310-32\lib\site-packages\selenium\webdriver\remote\utils.py", line 23, in dump_json
return json.dumps(json_struct)
File "C:\Users\elman\AppData\Local\Programs\Python\Python310-32\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Users\elman\AppData\Local\Programs\Python\Python310-32\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\elman\AppData\Local\Programs\Python\Python310-32\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\elman\AppData\Local\Programs\Python\Python310-32\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable

Related

Web3.py TypeError: Address <object> must be provided as a string

I have an issue using web3.py library. I'm trying to interact with a deployed smart contract on the Polygon (layer2) network.
Here is my python code:
def mintNFT(metaCID):
polygon_url = "https://rpc-mainnet.maticvigil.com"
web3 = Web3(Web3.HTTPProvider(polygon_url))
print ("BlockchainIsConnected :",web3.isConnected())
my_account = "MY_PRIVATE_KEY"
acct = web3.eth.account.from_key(my_account)
web3.eth.defaultAccount = acct
print (type(acct.address), type(acct.key)) #Returns str and str
abi_file = open('/Users/aa/Desktop/minty/data/abi/Minty.json','r')
abi = json.load(abi_file)
minty_contract_address = web3.toChecksumAddress("CONTRACT_ADDRESS")
contract = web3.eth.contract(address=minty_contract_address,abi=abi)
tx_hash = contract.functions.mintToken(acct.address,metaCID).transact()
print ("tx_hash =",web3.toHex(tx_hash))
web3.eth.waitForTransactionReceipt(tx_hash) #Waiting for transaction to be mined
I don't understand because acct.address is a string (my public address from my privatekey) 20 bytes long (without "Ox" as prefix).
Here is my Solidity code:
pragma solidity ^0.7.0;
import "hardhat/console.sol";
import "#openzeppelin/contracts/token/ERC721/ERC721.sol";
import "#openzeppelin/contracts/utils/Counters.sol";
contract Minty is ERC721 {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("TOKENNAME", "SYMBOL") {}
function mintToken(address owner, string memory metadataURI)
public
returns (uint256)
{
_tokenIds.increment();
uint256 id = _tokenIds.current();
_safeMint(owner, id);
_setTokenURI(id, metadataURI);
return id;
}
}
And here is the full error:
Traceback (most recent call last):
File "/Users/aa/Desktop/minty/scripts/test.py", line 80, in <module>
mintNFT(metaCID)
File "/Users/aa/Desktop/minty/scripts/test.py", line 76, in mintNFT
tx_hash = contract.functions.mintToken(acct.address,metaCID).transact()
File "/usr/local/lib/python3.9/site-packages/web3/contract.py", line 997, in transact
return transact_with_contract_function(
File "/usr/local/lib/python3.9/site-packages/web3/contract.py", line 1590, in transact_with_contract_function
txn_hash = web3.eth.send_transaction(transact_transaction)
File "/usr/local/lib/python3.9/site-packages/web3/eth.py", line 577, in send_transaction
return self._send_transaction(transaction)
File "/usr/local/lib/python3.9/site-packages/web3/module.py", line 53, in caller
(method_str, params), response_formatters = method.process_params(module, *args, **kwargs) # noqa: E501
File "/usr/local/lib/python3.9/site-packages/web3/method.py", line 201, in process_params
_apply_request_formatters(params, self.request_formatters(method)))
File "/usr/local/lib/python3.9/site-packages/eth_utils/functional.py", line 45, in inner
return callback(fn(*args, **kwargs))
File "/usr/local/lib/python3.9/site-packages/web3/method.py", line 51, in _apply_request_formatters
formatted_params = pipe(params, request_formatters)
File "cytoolz/functoolz.pyx", line 667, in cytoolz.functoolz.pipe
File "cytoolz/functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
File "cytoolz/functoolz.pyx", line 505, in cytoolz.functoolz.Compose.__call__
File "cytoolz/functoolz.pyx", line 254, in cytoolz.functoolz.curry.__call__
File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
File "/usr/local/lib/python3.9/site-packages/eth_utils/decorators.py", line 91, in wrapper
return ReturnType(result) # type: ignore
File "/usr/local/lib/python3.9/site-packages/eth_utils/applicators.py", line 22, in apply_formatter_at_index
yield formatter(item)
File "cytoolz/functoolz.pyx", line 254, in cytoolz.functoolz.curry.__call__
File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
File "/usr/local/lib/python3.9/site-packages/web3/_utils/rpc_abi.py", line 212, in apply_abi_formatters_to_dict
formatted_values = map_abi_data(
File "cytoolz/functoolz.pyx", line 254, in cytoolz.functoolz.curry.__call__
File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
File "/usr/local/lib/python3.9/site-packages/web3/_utils/abi.py", line 799, in map_abi_data
return pipe(data, *pipeline)
File "cytoolz/functoolz.pyx", line 667, in cytoolz.functoolz.pipe
File "cytoolz/functoolz.pyx", line 642, in cytoolz.functoolz.c_pipe
File "cytoolz/functoolz.pyx", line 254, in cytoolz.functoolz.curry.__call__
File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
File "/usr/local/lib/python3.9/site-packages/web3/_utils/abi.py", line 833, in data_tree_map
return recursive_map(map_to_typed_data, data_tree)
File "/usr/local/lib/python3.9/site-packages/web3/_utils/decorators.py", line 30, in wrapped
wrapped_val = to_wrap(*args)
File "/usr/local/lib/python3.9/site-packages/web3/_utils/formatters.py", line 89, in recursive_map
items_mapped = map_collection(recurse, data)
File "/usr/local/lib/python3.9/site-packages/web3/_utils/formatters.py", line 76, in map_collection
return datatype(map(func, collection))
File "/usr/local/lib/python3.9/site-packages/web3/_utils/formatters.py", line 88, in recurse
return recursive_map(func, item)
File "/usr/local/lib/python3.9/site-packages/web3/_utils/decorators.py", line 30, in wrapped
wrapped_val = to_wrap(*args)
File "/usr/local/lib/python3.9/site-packages/web3/_utils/formatters.py", line 90, in recursive_map
return func(items_mapped)
File "/usr/local/lib/python3.9/site-packages/web3/_utils/abi.py", line 830, in map_to_typed_data
return ABITypedData(func(*elements))
File "/usr/local/lib/python3.9/site-packages/web3/_utils/normalizers.py", line 78, in wrapper
modified = to_wrap(type_str, data)
File "/usr/local/lib/python3.9/site-packages/web3/_utils/normalizers.py", line 196, in abi_address_to_hex
validate_address(data)
File "/usr/local/lib/python3.9/site-packages/web3/_utils/validation.py", line 177, in validate_address
raise TypeError('Address {} must be provided as a string'.format(value))
TypeError: Address <eth_account.signers.local.LocalAccount object at 0x105ddb7c0> must be provided as a string
I'm using the 5.21.0 version of Web3.py.
I will not provide my ABI here because it's a very long json file. But let me know if you need it.
I could try to call the solidity function with a RawTransaction, but I don't know how to pass parameters inside.
try call RawTransaction it should work:
def mintNFT(metaCID):
polygon_url = "https://rpc-mainnet.maticvigil.com"
web3 = Web3(Web3.HTTPProvider(polygon_url))
print ("BlockchainIsConnected :",web3.isConnected())
my_account = "MY_PRIVATE_KEY"
my_address= Web3.toChecksumAddress(Web3.toHex(Web3.sha3(hexstr=str(keys.private_key_to_public_key(keys.PrivateKey(key))))[-20:]))
abi_file = open('/Users/aa/Desktop/minty/data/abi/Minty.json','r')
abi = json.load(abi_file)
minty_contract_address = web3.toChecksumAddress("CONTRACT_ADDRESS")
contract = web3.eth.contract(address=minty_contract_address,abi=abi)
tx = contract.functions.mintToken(acct.address,metaCID).buildTransaction({'from': my_address,'gasPrice': web3.toWei('5', 'gwei'),'nonce': web3.eth.getTransactionCount(my_address),'gas': 100000,})
signed_txn = web3.eth.account.signTransaction(approve, private_key=private_key_owner)
tx_token = web3.eth.sendRawTransaction(signed_txn.rawTransaction)
hash = web3.toHex(tx_token)
print("Transaction Hash: ",str(hash))
web3.eth.waitForTransactionReceipt(hash)
and specify gasPrice and gas by estimate functions or pass it manually.
web3.eth.estimate_gas()
In my case it was because I copy and pasted my address and forgot to put the quotes around it to make it a string.
my_address = 0x043...434fjk
after actually making sure i had quotes around it like so:
my_address = "0x043...434fjk"
it worked.
in the case where you are using an object you have to access the string within the address object

InstaLoader simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I've been trying to fix this but couldn't find a fix anywhere, I'm using InstaLoader 4.5.5.
I am getting this error:
Traceback (most recent call last):
File "main.py", line 145, in <module>
attemptRoutine()
File "main.py", line 136, in attemptRoutine
routine()
File "main.py", line 90, in routine
scrapeVideos(username = IG_USERNAME,
File "/media/ubuntu/EE81-0482/Python/auto_Yt/scrape_videos.py", line 18, in scrapeVideos
L.login(username, password)
File "/home/ubuntu/.local/lib/python3.8/site-packages/instaloader/instaloader.py", line 483, in login
self.context.login(user, passwd)
File "/home/ubuntu/.local/lib/python3.8/site-packages/instaloader/instaloadercontext.py", line 224, in login
resp_json = login.json()
File "/usr/lib/python3/dist-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I've been trying to scrape videos from meme pages for a youtube channel, Code taken from:
automated_youtube_channel Github

Python client for AWS Redis Cluster

Can anyone suggest a Python client for AWS Redis Cluster enabled?
I'm using redis-py-cluster, but it fails:
Sample code:
from rediscluster import StrictRedisCluster
startup_nodes = [{"host": "xxxx.clustercfg.apn2.cache.amazonaws.com", "port": "6379"}]
r = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True, skip_full_coverage_check=False)
r.set('foo', 'bar')
value = r.get('foo')
======
Exception:
Traceback (most recent call last):
File "testRedisCluster.py", line 11, in
r = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True, skip_full_coverage_check=False)
File "/Library/Python/2.7/site-packages/rediscluster/client.py", line 181, in init
**kwargs
File "/Library/Python/2.7/site-packages/rediscluster/connection.py", line 141, in init
self.nodes.initialize()
File "/Library/Python/2.7/site-packages/rediscluster/nodemanager.py", line 228, in initialize
need_full_slots_coverage = self.cluster_require_full_coverage(nodes_cache)
File "/Library/Python/2.7/site-packages/rediscluster/nodemanager.py", line 270, in cluster_require_full_coverage
return any(node_require_full_coverage(node) for node in nodes.values())
File "/Library/Python/2.7/site-packages/rediscluster/nodemanager.py", line 270, in
return any(node_require_full_coverage(node) for node in nodes.values())
File "/Library/Python/2.7/site-packages/rediscluster/nodemanager.py", line 267, in node_require_full_coverage
return "yes" in r_node.config_get("cluster-require-full-coverage").values()
File "/Library/Python/2.7/site-packages/redis/client.py", line 715, in config_get
return self.execute_command('CONFIG GET', pattern)
File "/Library/Python/2.7/site-packages/redis/client.py", line 668, in execute_command
return self.parse_response(connection, command_name, **options)
File "/Library/Python/2.7/site-packages/redis/client.py", line 680, in parse_response
response = connection.read_response()
File "/Library/Python/2.7/site-packages/redis/connection.py", line 629, in read_response
raise response
redis.exceptions.ResponseError: unknown command 'CONFIG'
I'm using redis-py-cluster 1.3.4.
Any idea?
Change the parameter skip_full_coverage_check=False to skip_full_coverage_check=True

google-api-python-client broken by OAuth2?

I am trying to check whether a certain dataset exists in BigQuery and I get this very strange error I've never had untill yesterday:
ERROR:dsUtils.bq_utils:Could not check if dataset tmp exists.
Traceback (most recent call last):
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\dsUtils\bq_utils.py", line 113, in _get
resp = bq_service.datasets().get(projectId=self.project_id, datasetId=self.id).execute(num_retries=2)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\oauth2client\util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\googleapiclient\http.py", line 755, in execute
method=str(self.method), body=self.body, headers=self.headers)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\googleapiclient\http.py", line 93, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\oauth2client\client.py", line 598, in new_request
self._refresh(request_orig)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\oauth2client\client.py", line 864, in _refresh
self._do_refresh_request(http_request)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\oauth2client\client.py", line 891, in _do_refresh_request
body = self._generate_refresh_request_body()
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\oauth2client\client.py", line 1597, in _generate_refresh_request_body
assertion = self._generate_assertion()
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\oauth2client\service_account.py", line 318, in _generate_assertion
key_id=self._private_key_id)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\oauth2client\crypt.py", line 97, in make_signed_jwt
signature = signer.sign(signing_input)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\oauth2client\_pycrypto_crypt.py", line 101, in sign
return PKCS1_v1_5.new(self._key).sign(SHA256.new(message))
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\Crypto\Signature\PKCS1_v1_5.py", line 112, in sign
m = self._key.decrypt(em)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\Crypto\PublicKey\RSA.py", line 174, in decrypt
return pubkey.pubkey.decrypt(self, ciphertext)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\Crypto\PublicKey\pubkey.py", line 93, in decrypt
plaintext=self._decrypt(ciphertext)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\Crypto\PublicKey\RSA.py", line 235, in _decrypt
r = getRandomRange(1, self.key.n-1, randfunc=self._randfunc)
File "C:\Users\paco\Anaconda3\envs\visitForecastEnv\lib\site-packages\Crypto\PublicKey\RSA.py", line 126, in __getattr__
raise AttributeError("%s object has no %r attribute" % (self.__class__.__name__, attrname,))
AttributeError: _RSAobj object has no '_randfunc' attribute
Has anyone an idea of why I get these errors suddenly?

Invalid tag name - django tastypie

I keep getting the following error:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
response = wrapped_callback(request, callback_args, *callback_kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.6.0.5/newrelic/hooks/framework_django.py", line 485, in wrapper
return wrapped(args, *kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.6.0.5/newrelic/hooks/component_tastypie.py", line 49, in inner_fn_wrapper
return inner_fn(args, *kwargs)
File "/app/.heroku/src/django-tastypie-master/tastypie/api.py", line 78, in wrapper
return getattr(self, view)(request, args, *kwargs)
File "/app/.heroku/src/django-tastypie-master/tastypie/api.py", line 155, in top_level
serialized = self.serializer.serialize(available_resources, desired_format, options)
File "/app/.heroku/src/django-tastypie-master/tastypie/serializers.py", line 203, in serialize
serialized = getattr(self, "to_%s" % desired_format)(bundle, options)
File "/app/.heroku/src/django-tastypie-master/tastypie/serializers.py", line 410, in to_xml
return tostring(self.to_etree(data, options), xml_declaration=True, encoding='utf-8')
File "/app/.heroku/src/django-tastypie-master/tastypie/serializers.py", line 294, in to_etree
element.append(self.to_etree(value, options, name=key, depth=depth+1))
File "/app/.heroku/src/django-tastypie-master/tastypie/serializers.py", line 291, in to_etree
element = Element(name or 'object')
File "lxml.etree.pyx", line 2841, in lxml.etree.Element (src/lxml/lxml.etree.c:66367)
File "apihelpers.pxi", line 110, in lxml.etree._makeElement (src/lxml/lxml.etree.c:14784)
File "apihelpers.pxi", line 1573, in lxml.etree._tagValidOrRaise (src/lxml/lxml.etree.c:28777)
ValueError: Invalid tag name u'user/photos'
The error comes from serializers.py:
if lxml is None:
raise ImproperlyConfigured("Usage of the XML aspects requires lxml and defusedxml.")
return tostring(self.to_etree(data, options), xml_declaration=True, encoding='utf-8')
Does anyone have a clue how to solve this?

Categories