Introduction

Installation

Frequently asked questions

Syntax, keywords and built-in functions

Standard distribution

import implementation

Browser interface

Brython-specific built-in modules

Working with Brython

Brython for Firefox OS

Cookbook

 

Elements attributes and methods

The elements in a page have attributes and methods that depend on the element type ; they are defined by the W3C and can be found on many Internet sites.

Since their name may vary depending on the browser, Brython defines additional attributes that work in all cases :

NameTypeDescriptionR = read only
R/W = read + write
abs_leftintegerposition of the element relatively to the window left borderR
abs_topintegerposition of the element relatively to the window top borderR
childrenlistthe element's children in the document treeR
class_namestringthe name of the element's class (tag attribute class)R/W
clearmethodelt.clear() removes all the descendants of the element-
closest method elt.closest(tag_name) returns the first parent element of elt with the specified tag name. Raises KeyError if no element is found. -
getmethodselects elements (cf access to elements)-
heightintegerelement height in pixels (2)R/W
htmlstringthe HTML code inside the element R/W
index method elt.index([selector]) returns the index (an integer) of the element among its parent's children. If selector is specified, only the elements matching the CSS selector are taken into account ; in this case, if no element matches, the method returns -1. -
insidemethodelt.inside(other) tests if elt is contained inside element other-
leftintegerthe position of the element relatively to the left border of the first positioned parent (1)R/W
parentDOMNode instancethe element's parent (None for doc)R
selectmethodelt.select(css_selector) returns the elements matching the specified CSS selector-
select_one method elt.select_one(css_selector) returns the elements matching the specified CSS selector, otherwise None -
textstringthe text inside the elementR/W
topintegerthe position of the element relatively to the upper border of the first positioned parent (1)R/W
widthintegerelement width in pixels (2)R/W

(1) When going up the DOM tree, we stop at the first parent whose attribute style.position is set to a value different of "static". left and top are computed like style.left and style.top but are integer, not strings ending with px.

(2) Same as style.height and style.width but as integers.

To add a child to an element, use the operator <= (think of it as a left arrow for assignment)

from browser import document, html
document['zone'] <= html.INPUT(Id="data")

Iterating on an element's children can be done using the usual Python syntax :

for child in element:
    (...)

To destroy an element, use the keyword del

zone = document['zone']
del zone

The options collection associated with a SELECT object has an interface of a Python list :

  • access to an option by its index : option = elt.options[index]
  • insertion of an option at the index position : elt.options.insert(index,option)
  • insertion of an option at the end of the list : elt.options.append(option)
  • deleting an option : del elt.options[index]