Anas
Anas

Reputation: 742

Python Object of type WebElement is not JSON serializable

I want convert dict to json using json.dumps but i have problem with WebElement.

I got TypeError: Object of type WebElement is not JSON serializable

This my example code :

def x():
  p = {'a':'a','b':driver.find_element(By.XPATH, xpath)}
  return p
dict = x()
print(json.dumps(dict))

I want convert json except WebElement but without modify x function.

Upvotes: 1

Views: 4414

Answers (2)

Anas
Anas

Reputation: 742

solved using json.dumps(dict, default=lambda o: '<not serializable>') from https://stackoverflow.com/a/51674892/12716228

Upvotes: 2

zafer durkut
zafer durkut

Reputation: 11

Your element object is not serializable. if you can convert the element object to a string. After you can set to b key.

element = str(driver.find_element(By.XPATH, xpath))
dict = {'a':'a','b':element}
print(json.dumps(dict))

Upvotes: 0

Related Questions