
[英]Python/Flask - Object of type "TypeError' is not JSON serializable
[英]Python Object of type WebElement is not JSON serializable
我想使用 json.dumps 將 dict 轉換為 json 但我對 WebElement 有問題。
我得到TypeError: Object of type WebElement is not JSON serializable
這是我的示例代碼:
def x():
p = {'a':'a','b':driver.find_element(By.XPATH, xpath)}
return p
dict = x()
print(json.dumps(dict))
我想轉換 json 除了 WebElement 但不修改 x function。
您的元素 object 不可序列化。 如果您可以將元素 object 轉換為字符串。 之后可以設置為b鍵。
element = str(driver.find_element(By.XPATH, xpath))
dict = {'a':'a','b':element}
print(json.dumps(dict))
使用 https ://stackoverflow.com/a/51674892/12716228中json.dumps(dict, default=lambda o: '<not serializable>')
解決
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.