1

On a high level, does anyone know how to enter the Immersive Reader mode on Microsoft Edge when it is available for a given webpage through Selenium?

My aim is to load up a page, enter Immersive Reader, and save the page's source code to disk. I'm firing up Edge through Docker and I'm pragmatically connecting to it via a Node.js script.

I've tried driver.actions().sendKeys(KEY.F9), but that doesn't work since I'm targeting the browser and not a DOM element.

Many thanks for all your help.

G-R
  • 75
  • 7

2 Answers2

1

New

Just run

driver.get('read://' + url)

and the site is loaded in immersive reader mode if available.

Old

To interact with the UI you have to use pyautogui (pip install pyautogui) and then run this code while the browser window is on focus/active:

import pyautogui
pyautogui.press('F9')

It is also useful for example to save a pdf by interacting with the popup window appearing when pressing CTRL+S.

sound wave
  • 3,191
  • 3
  • 11
  • 29
  • Interesting. I'll give it a try, but I'm not sure it will scale well given browser resolution becomes important for this technique. I'd ideally like to be able to run this headless in the future. – G-R Jan 18 '23 at 15:25
  • @G-R it seems like it is enough to do `driver.get('read://' + url)` – sound wave Jan 18 '23 at 15:42
  • It isn't working for me; when you do this then print to PDF the page is blank – Colton Campbell Mar 03 '23 at 00:08
  • Nevermind it is working! note that this ONLY works in `--headless` mode (probably because I believe printtoPDF only works in headless mode) – Colton Campbell Mar 03 '23 at 04:31
  • @ColtonCampbell are you talking about `driver.get('read://' + url)`? to me it works in non headless mode – sound wave Mar 03 '23 at 07:50
  • @ColtonCampbell That's interesting because I can only driver.get('read://' + url) in headful mode. It doesn't work in headless for me! – G-R Mar 03 '23 at 07:59
0

Here's a bit of code for anyone else who might stumble across this:

Credits to @sound wave for helping me get there!

const { Builder } = require('selenium-webdriver');
const fs = require('fs');

(async () => {

    const driver = await new Builder().forBrowser('MicrosoftEdge').usingServer('http://localhost:4444').build();

    await driver.get('read://https://www.bbc.co.uk/news/entertainment-arts-64302120'); // this URL needs to be Immersive Reader supported

    await driver.switchTo().frame(0);
    const pagesource = await driver.getPageSource();

    fs.writeFile('test.html', pagesource, err => {
        if (err) {
            console.log(err);
        }
    });

    const title = (await driver.getTitle()).trim();
    console.log(title);

    await driver.quit();

})().catch((e) => console.error(e));
G-R
  • 75
  • 7