Getting "Element not found" error with xpath in Odoo view. What's the cause and fix?

Getting "Element not found" error with xpath in Odoo view. What's the cause and fix?

Publish Date: Jul 7
0 0

Step-by-Step Solution

Step 1: Understand the Error

The error means that your xpath is trying to match an element that does not exist in the view you are inheriting.
Wrong XPath Example:
xml

<xpath expr="//div[@name='button_box']" position="inside">
    <button name="action_confirm" string="Confirm" type="object"/>
</xpath>
Enter fullscreen mode Exit fullscreen mode

div[@name='button_box'] is not present in the original view.

** Step 2:** Locate the Correct Target View and XPath

  1. Activate Developer Mode in Odoo.
  2. Open the form view you're trying to modify.
  3. Click on "View → Edit View: Form".
  4. Look at the XML structure — find the correct element and its attributes (name, class, etc.). Sample Existing View: XML
<field name="state" widget="statusbar" statusbar_visible="draft,confirmed"/>
Enter fullscreen mode Exit fullscreen mode

You want to insert a button after this field.
### Step 3: Correct Inheritance View Code
Here’s how you properly inherit and insert a new button:

<record id="view_order_form_inherit" model="ir.ui.view">
    <field name="name">sale.order.form.inherit.button</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <xpath expr="//header" position="inside">
            <button name="custom_action"
                    string="Custom Button"
                    type="object"
                   class="btn btn-primary"/>
        </xpath>
    </field>
</record>``
Enter fullscreen mode Exit fullscreen mode

-inherit_id refers to the original view.

  • xpath targets the <header> tag.
  • Button is inserted inside header using position="inside". ### Step 4: Add Python Method In your model (sale.order), define the action: Python
from odoo import models

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    def custom_action(self):
        # your logic here
        return {
            'type': 'ir.actions.client',
            'tag': 'display_notification',
            'params': {
                'title': 'Custom Button Clicked',
                'type': 'success',
                'message': 'This is a custom action.',
                'sticky': False,
            }
        }
Enter fullscreen mode Exit fullscreen mode

Step 5: Upgrade the Module

After editing XML and Python:
Bash

$ odoo -u your_module_name -d your_database
Enter fullscreen mode Exit fullscreen mode

Or use Odoo UI:

  • Apps → Update Apps List → Upgrade your module.

In Odoo implementation, when working with XPath in views, issues can occur when the target elements do not exist in the original XML, resulting in errors like "Element not found." This typically happens when the XPath expression fails to match the structure of the inherited view. To resolve this, ensure the XPath expression accurately references an existing element and verify the correct view inheritance. Additionally, it's crucial to use precise XPath expressions that target elements based on their attributes or position in the view hierarchy, ensuring smooth customization and extension of Odoo's default views.

Comments 0 total

    Add comment