1

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.

Anas Fanani
  • 685
  • 7
  • 19

2 Answers2

1

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

Anas Fanani
  • 685
  • 7
  • 19
0

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))