From 7b9319df5a272f65e6087f8c3fbef91302c2facb Mon Sep 17 00:00:00 2001 From: Crend King <975235+CrendKing@users.noreply.github.com> Date: Wed, 9 Aug 2023 19:44:22 -0700 Subject: [PATCH] Add get_attribute_value() for Element --- src/browser/tab/element/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/browser/tab/element/mod.rs b/src/browser/tab/element/mod.rs index 6d52051b..ed04cd1a 100644 --- a/src/browser/tab/element/mod.rs +++ b/src/browser/tab/element/mod.rs @@ -486,6 +486,18 @@ impl<'a> Element<'a> { Ok(description.attributes) } + pub fn get_attribute_value(&self, attribute_name: &str) -> Result> { + let js_fn = format!("function() {{ return this.getAttribute('{attribute_name}'); }}"); + + Ok( + if let Some(attribute_value) = self.call_js_fn(&js_fn, Vec::new(), true)?.value { + Some(serde_json::from_value(attribute_value)?) + } else { + None + } + ) + } + /// Get boxes for this element pub fn get_box_model(&self) -> Result { let model = self