10
$\begingroup$

For example one can get WebElementObject like so:

el = WebExecute[{"OpenPage" -> "mathematica.stackexchange.com", 
   "LocateElements" -> "XPath" -> "/html/body/header[2]/div/a"}][[2,1]]

Right now it seems the only way to extract element attributes (e.g. href for <a> tag) with WebExecute framework, is to write some JavascriptExecute query. Is there a more straight forward solution?

$\endgroup$

2 Answers 2

9
$\begingroup$

As of 12.3 (I haven't checked the earlier versions), there is an undocumented command ElementAttribute to extract attributes:

session = StartWebSession[]; 
WebExecute["OpenPage" -> "https://wolfram.com"]; 
elements = WebExecute["LocateElements" -> "CSSSelector" -> "div > a.item-0"]; 
WebExecute["ElementAttribute" -> {First@elements, "href"}]
(* "https://blog.wolfram.com/2021/11/30/six-reasons-why-the-wolfram-language-is-like-open-source/?source=frontpage-news" *)

How did I discover it, you ask? Well, it's one of the commands that is defined in /Applications/Wolfram Desktop.app/Contents/SystemFiles/Components/WebUnit/Kernel/wdElement.m, so I just tried it and it seems to work - probably an omission on Wolfram Research's part to not include it in the documentation.

$\endgroup$
7
$\begingroup$

After some spelunking into WebUnit`Private, I've found some helpful functions like get, fetch and attribute, that could be used to do exactly what I need:

getAttribute[element_WebElementObject, attribute_String] := 
 getAttribute[$CurrentWebSession, element, attribute]

getAttribute[session_WebSessionObject, element_WebElementObject, attribute_String] := 
 With[{sessionInfo = session /@ {"SessionID", "Browser", "URL"}}, 
  WebUnit`Private`attribute[sessionInfo, element["ElementId"], attribute]]

Hope it would be included in some form as an official documented functionallity in the future.

$\endgroup$
2
  • $\begingroup$ I use this function all the time. Have you considered submitting it to the Wolfram Function Repository? $\endgroup$
    – kickert
    Commented Aug 25, 2021 at 13:33
  • 2
    $\begingroup$ @kickert, see my answer below - the functionality is there, it just looks like a documentation omission. I will submit a bug report to WR. $\endgroup$
    – Victor K.
    Commented Dec 10, 2021 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.