3
$\begingroup$

WebExecute allows to control a web browser through Mathematica. There are various actions one can perform programmatically through this function. However, I could not find the command to extract the website source text (the output you get by right-clicking on any website and selecting "View page source") with this function. Is it possible to obtain the page source of a website currently loaded by a web session in Mathematica 12.3? If so, which syntax can I use to do this?

As an example:

Let's say, we start a session and load a website:

session = StartWebSession[];
WebExecute["OpenPage" -> "https://www.wolfram.com"];

I would then like to get the page source, something like:

pageSource = GetPageSource[session];

Except, GetPageSource is not a real command. What should I write instead of GetPageSource?

$\endgroup$
2
  • 1
    $\begingroup$ maybe Import[WebExecute[session, "PageURL"], "Source"]? $\endgroup$
    – kglr
    Commented Sep 9, 2021 at 0:36
  • $\begingroup$ @kglr Yes, that seems to work! $\endgroup$
    – Kagaratsch
    Commented Sep 9, 2021 at 2:06

1 Answer 1

3
$\begingroup$

We can get the raw HTML source as a single string using Import with "Source" as the second argument:

pageSource = Import[WebExecute[session, "PageURL"], "Source"]
$\endgroup$

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.