【问题标题】: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如何在 python selenium 中解决此错误我收到的错误消息是 TypeError: Object of type method is not JSON serializable
【发布时间】:2020-10-05 11:36:35
【问题描述】:

单击父菜单下的子菜单时出现此错误,请帮我解决此问题,脚本调用“子菜单方法”时发生错误

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

我能看到的例外是

默认默认值(自我,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

enter image description here

【问题讨论】:

  • 什么是CreateWorkflow?我应该知道这个吗?听起来不一定是 Selenium 中的错误。向我们展示堆栈跟踪。
  • hI @Booboo CreateWorkflow 是一个类,我在其中定义了网页类 CreateWorkflow(workflow_locator): def __init__(self, driver): self.driver = driver def click_workflow_sub_menu(self) 的操作方法: self.driver.find_element_by_xpath(self.click_workflow_sub_menu).click()
  • 最好将该代码与堆栈跟踪一起放在您的问题中
  • 嗨@Booboo,请立即检查代码
  • 请添加完整的堆栈跟踪

标签: python selenium pytest pom.xml


【解决方案1】:

你有:

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

当您致电self.click_workflow_sub_menu() 时,会发生以下情况:

  1. self.click_workflow_sub_menu 被评估。但这会计算为绑定方法,而不是有效的 xpath 表达式。
  2. self.driver.find_element_by_xpath 使用第 1 步的结果调用,该结果不是有效的 xpath 表达式,并导致异常。

我相信这是你的问题。你的意思可能是:

def click_workflow_sub_menu(self):
    self.driver.find_element_by_xpath(self.workflow_submenu_xpath).click()

【讨论】:

    猜你喜欢
    • 2017-10-14
    • 2021-09-15
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 2023-02-24
    • 2018-01-26
    相关资源
    最近更新 更多