[英]How to solve this error in python selenium the error message that I am getting is TypeError: Object of type method is not JSON serializable
I am getting this error when clicking on a submenu under a parent menu, please help me out to solve this problem, The error occurs when the script calls the 'Submenu method'单击父菜单下的子菜单时出现此错误,请帮我解决此问题,脚本调用“子菜单方法”时发生此错误
enter code here
class workflow_locator:
# Create a new workflow locator
dropdown_xpath = "(//DIV[@class='btn-group dropdown'])[1]"
admin_menu_xpath = "(//A[@class='dropdown-item'])[3]"
breadcrumb_xpath = "(//DIV[@class='left-menu-toggle'])[1]"
workflow_menu_xpath = "(//A[@_ngcontent-ng-cli-universal-c5=''])[31]"
workflow_submenu_xpath = "(//A[@href='/admin/workflows'])"
new_btn_xpath = "(//DIV[text()=' New'])"
workflow_name_xpath = "(//INPUT[@id='name'])[1]"
workflow_description_xpath = "(//TEXTAREA[@id='description'])[2]"
active_toggle_xpath = "(//SPAN[@class='slider'])"
add_button_xpath = "(//BUTTON[@class='btn btn-primary btn-sm'])[text()='Add']"
#This class define the action method of the below test class
from Resources.Workflow_locator import workflow_locator
class CreateWorkflow(workflow_locator):
def __init__(self, driver):
self.driver = driver
def click_drop_down_menu(self):
self.driver.find_element_by_xpath(self.dropdown_xpath).click()
def select_admin_from_the_drop_down_menu(self):
self.driver.find_element_by_xpath(self.admin_menu_xpath).click()
def click_breadcrumb_menu(self):
self.driver.find_element_by_xpath(self.breadcrumb_xpath).click()
def click_workflow_menu(self):
self.driver.find_element_by_xpath(self.workflow_menu_xpath).click()
def click_workflow_sub_menu(self):
self.driver.find_element_by_xpath(self.click_workflow_sub_menu).click()
# This is a test class
def test_workflow(self, setup):
self.driver = setup
self.driver.get(self.base_url)
self.driver.maximize_window()
self.lp = Login(self.driver)
self.lp.set_Username(self.username)
self.lp.click_next_btn()
self.lp.set_password(self.Password)
self.lp.click_signin_btn()
self.lp.click_confirm_btn()
self.driver.get_screenshot_as_file(".\\Screenshoots\\login.png")
self.logger.info("############ User is successfully logged in ########### ")
print("Login is successfully completed")
self.logger.info("######### Creating WorkFlow #########")
self.wf=CreateWorkflow(self.driver)
self.wf.click_drop_down_menu()
self.wf.select_admin_from_the_drop_down_menu()
self.wf.click_breadcrumb_menu()
self.wf.click_workflow_menu()
self.wf.click_workflow_sub_menu()
The exception I can see is我能看到的例外是
def default(self, o): """Implement this method in a subclass such that it returns a serializable object for o
, or calls the base implementation (to raise a TypeError
). def default(self, o): """在子类中实现这个方法,以便它为
o
返回一个可序列化的对象,或者调用基本实现(引发TypeError
)。
For example, to support arbitrary iterators, you could
implement default like this::
def default(self, o):
try:
iterable = iter(o)
except TypeError:
pass
else:
return list(iterable)
# Let the base class default method raise the TypeError
return JSONEncoder.default(self, o)
"""
raise TypeError(f'Object of type {o.__class__.__name__} '
f'is not JSON serializable')
E TypeError: Object of type method is not JSON serializable E TypeError: 类型方法的对象不是 JSON 可序列化的
You have:你有:
def click_workflow_menu(self):
self.driver.find_element_by_xpath(self.workflow_menu_xpath).click()
def click_workflow_sub_menu(self):
self.driver.find_element_by_xpath(self.click_workflow_sub_menu).click()
When you call self.click_workflow_sub_menu()
, this is what happens:当您调用
self.click_workflow_sub_menu()
,会发生以下情况:
self.click_workflow_sub_menu
is evaluated. self.click_workflow_sub_menu
被评估。 But this evaluates to a bound method and not a valid xpath expression.self.driver.find_element_by_xpath
is invoked with the result from step 1, which is not a valid xpath expression, and an exception results. self.driver.find_element_by_xpath
使用步骤 1 的结果调用,该结果不是有效的 xpath 表达式,并导致异常。 I believe that is your problem.我相信这是你的问题。 What you probably meant was:
你的意思可能是:
def click_workflow_sub_menu(self):
self.driver.find_element_by_xpath(self.workflow_submenu_xpath).click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.