🧱 Working with Elements
Fuyu gives you powerful tools to find and interact with elements in the browser — just like a human would.
This guide covers how to select elements and use the Element
object to perform common actions like clicking, typing, and reading content.
🔍 Finding Elements
Use these two commands to locate elements on the page:
el = find_element("selector")
elements = find_elements("selector")
find_element()
returns the first match, orNone
if no match is found (it will not crash).find_elements()
returns a list of matching elements, or an empty list.
You can use any selector type:
- CSS:
#login-button
,.product-title
,div.item
- Pseudo selectors:
:has(...)
,:nth-child(...)
- XPath:
//button[@type="submit"]
Fuyu automatically detects the selector type.
🧩 The Element Object
When you use find_element()
or find_elements()
, you get an Element
object that represents a specific node on the page.
📄 Read Element Text
el = find_element(".title")
if el
print(el.text)
end
🖱 Click an Element
el = find_element("button.submit")
if el
el.click()
end
Other mouse actions:
el.double_click()
el.right_click()
el.hover()
⌨️ Type into Inputs
el = find_element("input[name='email']")
if el
el.input("[email protected]")
end
Keyboard actions:
el.enter()
el.backspace()
el.clear()
⌨️ Sending Key Presses
Fuyu exposes a global Keys
object for simulating keyboard input:
el.send_keys(Keys.ENTER)
el.send_keys(Keys.CTRL + "a")
el.send_keys(Keys.SHIFT + "test")
You can combine keys like Keys.CTRL + Keys.SHIFT
or use them with characters. This is useful for advanced interactions like shortcuts, selects, or hotkeys.
🔍 Read Attributes
el = find_element("img")
if el
print(el.src)
print(el.alt)
end
You can access any valid attribute using dot notation: el.href
, el.value
, etc.
🧭 Scroll Element
el = find_element(".scroll-box")
if el
el.scroll_into_view()
el.scroll_y(200)
end
You can also scroll within the element:
el.scroll(y, x)
el.scroll_x()
🔁 Looping Over Multiple Elements
When you use find_elements()
, you get a list of matching elements you can iterate:
items = find_elements(".product")
for item in items
print(item.text)
end
You can use this to extract lists, click all buttons in a section, or gather text from repeated elements.
⚠️ Best Practices
-
Always use
wait_for()
before trying to access elements that may not appear immediately. -
Use unique selectors like
#id
ordata-*
attributes when possible to avoid accidental matches.-
find_element()
returnsNone
if no matching element is found — this won't crash your script. However, attempting to access properties or call methods on None will result in a logged error and may lead to unexpected behavior.To avoid this, always check if the result is not
None
before using it.
Note: These errors are non-fatal and the script will continue running unless you explicitly stop it with something like
abort()
. -