Usage Examples
This page provides detailed examples of how to use BrowserJQuery for common browser automation tasks.
Basic Element Selection
Finding Elements by Selector
# Find all elements with a specific class
elements = browser.find(".my-class")
# Find elements with multiple classes
elements = browser.find(".class1.class2")
# Find elements by ID
element = browser.find("#my-id")
# Find elements by tag name
divs = browser.find("div")
# Find elements by attribute
elements = browser.find("[data-test='value']")
Finding Elements by Text
# Find elements containing specific text
elements = browser.find_elements_with_text("Click me")
# Find elements with exact text match
element = browser.find_lowest_element_with_text("Submit", exact_match=True)
# Find elements matching both selector and text
elements = browser.find_elements_with_selector_and_text(
selector="button",
text="Submit",
exact_match=True
)
Element Traversal
Element State and Properties
Checking Element State
# Check visibility
if element.is_visible():
print("Element is visible")
# Check if element has class
if element.has_class("active"):
print("Element is active")
# Check if element matches selector
if element.matches_selector(".my-class"):
print("Element matches selector")
# Check if element has matching descendants
if element.has(".child-class"):
print("Element has matching children")
Working with Form Elements
# Check if checkbox is checked
if checkbox.is_checked():
print("Checkbox is checked")
# Check if element is disabled
if button.is_disabled():
print("Button is disabled")
Getting Element Properties
# Get text content
text = element.text()
# Get HTML content
html = element.html()
# Get attribute value
href = element.attr("href")
data_value = element.attr("data-test")
# Get multiple attributes
attributes = {
"href": element.attr("href"),
"class": element.attr("class"),
"id": element.attr("id")
}
Working with Collections
Collection Operations
# Get number of elements
count = len(elements)
# Iterate over elements
for element in elements:
print(element.text())
# Get element by index
first = elements[0]
last = elements[-1]
# Get first and last elements
first = elements.first()
last = elements.last()
# Get all elements as list
element_list = elements.items()
Filtering Collections
# Filter elements by text
matching_elements = [
element for element in elements
if element.text() == "Expected Text"
]
# Filter elements by attribute
matching_elements = [
element for element in elements
if element.attr("data-test") == "value"
]
jQuery Integration
Managing jQuery Injection
# Check if jQuery is available
if browser.is_jquery_injected:
print("jQuery is available")
# Manually inject jQuery
browser.inject_jquery(by="file") # From local file
browser.inject_jquery(by="cdn") # From CDN
# Wait for jQuery to be ready
time.sleep(2) # After injection
Custom jQuery Scripts
# Execute custom jQuery script
result = browser.execute("""
return $(arguments[0]).find('.child').map(function() {
return $(this).text();
}).get();
""", element)