How to enter Immersive Reader on Microsoft Edge through Selenium?

310 Views Asked by At

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.

2

There are 2 best solutions below

6
sound wave On BEST ANSWER

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.

0
G-R On

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));