Learn how to interact with page elements - clicking, typing, selecting, and more
Nodriver provides powerful methods for interacting with web page elements. This guide covers the essential techniques for finding and manipulating DOM elements.
Use select() for a single element or select_all() for multiple elements:
import nodriver as ucbrowser = await uc.start()tab = await browser.get('https://example.com')# Find single elementsearch_box = await tab.select('input[type=text]')# Find all elementsall_links = await tab.select_all('a[href]')
Both select() and select_all() will retry for up to 10 seconds (configurable via timeout parameter) if the element isn’t immediately found, making them great for waiting for dynamic content.
Use find() to locate elements containing specific text:
# Find element by text (returns first match)login_button = await tab.find('Login')# Use best_match for more accurate resultslogin_button = await tab.find('Login', best_match=True)
The best_match=True flag finds the element with the most similar text length, helping you get the right “Login” button instead of scripts or metadata containing “login”.
Use find_all() to get all elements containing specific text.
async def send_keys(self, text: str): """ send text to an input field, or any other html element. hint, if you ever get stuck where using click() does not work, sending the keystroke \\n or \\r\\n or a spacebar work wonders! """ await self.apply("(elem) => elem.focus()") [ await self._tab.send(cdp.input_.dispatch_key_event("char", text=char)) for char in list(text) ]
# Query all optionsselect_element = await tab.select('select#country')options = await select_element.query_selector_all('option')# Select specific optionfor option in options: if option.text == 'United States': await option.select_option() break
From element.py:749-766:
async def select_option(self): """ for form (select) fields. when you have queried the options you can call this method on the option object. Calling option.select_option() will use that option as selected value. """ if self.node_name == "OPTION": await self.apply( """ (o) => { o.selected = true ; o.dispatchEvent(new Event('change', {view: window,bubbles: true})) } """ )
position = await element.get_position()print(f"Element at x={position.x}, y={position.y}")print(f"Size: {position.width}x{position.height}")print(f"Center: {position.center}")
link = await tab.select('a')print(link.href) # Access href attributeprint(link.text) # Get text contentprint(link.class_) # Get class attribute# Get all attributesprint(link.attrs)
element = await tab.select('custom-element')shadow_children = element.shadow_childrenif shadow_children: for child in shadow_children: print(child.tag_name)