use std::time::Duration;
use computeruse::{Browser, Desktop};
use tokio::time::sleep;
use tracing::info;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
info!("đ Simple Dataiku Element Extraction Example");
let desktop = Desktop::new(false, true)?;
let target_url = "https://pages.dataiku.com/guide-to-ai-agents";
println!("đ Opening Dataiku page: {target_url}");
let browser_element = desktop.open_url(target_url, Some(Browser::Edge))?;
println!("âŗ Waiting for page to load...");
sleep(Duration::from_secs(5)).await;
println!("\nđ Example 1: Getting page title");
let title_script = "document.title";
match browser_element.execute_browser_script(title_script).await {
Ok(title) => println!("đ Page title: {title}"),
Err(e) => println!("â Error: {e}"),
}
println!("\nđ Example 2: Getting Dataiku form element HTML");
let form_script = r#"
const formElement = document.getElementById('hs_form_target_form_735002917');
if (formElement) {
formElement.outerHTML.substring(0, 500) + '...';
} else {
'Form element not found';
}
"#;
match browser_element.execute_browser_script(form_script).await {
Ok(html) => {
if html.contains("not found") {
println!("âšī¸ Form element not found on this page");
} else {
println!("đ Found form element!");
println!("đ HTML: {html}");
}
}
Err(e) => println!("â Error: {e}"),
}
println!("\nđ Example 3: Getting hero banner element");
let hero_script = r#"
const heroElement = document.querySelector('.hero-banner__title');
if (heroElement) {
'Found: ' + heroElement.textContent.substring(0, 100);
} else {
'Hero banner not found';
}
"#;
match browser_element.execute_browser_script(hero_script).await {
Ok(result) => println!("đ Hero banner: {result}"),
Err(e) => println!("â Error: {e}"),
}
println!("\nđ Example 4: Comprehensive page analysis");
let analysis_script = r#"
JSON.stringify({
title: document.title,
url: window.location.href,
totalForms: document.querySelectorAll('form').length,
hsFormElements: document.querySelectorAll('[id*="hs_form"]').length,
hasTargetForm: document.getElementById('hs_form_target_form_735002917') !== null,
hasHeroTitle: document.querySelector('.hero-banner__title') !== null
}, null, 2)
"#;
match browser_element
.execute_browser_script(analysis_script)
.await
{
Ok(analysis) => {
println!("đ Page analysis:");
println!("{analysis}");
}
Err(e) => println!("â Error: {e}"),
}
println!("\nđ Example 5: Custom element extraction");
let custom_script = r#"
// Find all interesting elements and return their info
const elements = [];
// Look for forms
document.querySelectorAll('form').forEach(form => {
elements.push({
type: 'form',
id: form.id || 'no-id',
action: form.action || 'no-action'
});
});
// Look for buttons
document.querySelectorAll('button').forEach(button => {
elements.push({
type: 'button',
text: button.textContent.substring(0, 50),
id: button.id || 'no-id'
});
});
JSON.stringify({
timestamp: new Date().toISOString(),
elementsFound: elements.length,
elements: elements.slice(0, 10) // First 10 elements
}, null, 2)
"#;
match browser_element.execute_browser_script(custom_script).await {
Ok(result) => {
println!("đ Custom extraction result:");
println!("{result}");
}
Err(e) => println!("â Error: {e}"),
}
println!("\n⨠Example completed!");
println!("\nđ§ Key benefits of this approach:");
println!(" â
Single function: element.execute_browser_script()");
println!(" â
No remote debugging port setup needed");
println!(" â
Works with any JavaScript - you write the extraction logic");
println!(" â
Direct keyboard automation - reliable and simple");
println!(" â
Returns results as strings ready to use");
Ok(())
}