etaoin 2024-11-16

Hi all! Is there a possibility using etaoin to switch on Notifications on page and somehow intercept them to check contents? For interception I just need to run some JS, that looks possible. But what about permissions?

I think you'd probably enable them in some browser profile setting? A google for "selenium enable notifications" might yield some answers/ideas.

Playwrite supports it when creating Page instance (or Browser ?). Anyway it looks possible to enable with js.. need to check

I've not used Playwrite, but I have heard it is much more flexible than the comparatively rigid WebDriver.

‘’’ (ns playwright-example.core (:import [com.microsoft.playwright Playwright$create Browser Browser$NewContextOptions BrowserContext Page])) (defn enable-notifications [] (with-open [playwright (Playwright/create)] ;; Launch Chromium browser (let [browser (.chromium playwright) launch-options (doto (com.microsoft.playwright.BrowserType$LaunchOptions.) (.setHeadless false)) ; Set to true to run headless browser-instance (.launch browser launch-options) ;; Set up new context with notifications permission context-options (doto (Browser$NewContextOptions.) (.setPermissions ["notifications"])) context (.newContext browser-instance context-options) ;; Open a new page page (.newPage context)] ;; Override the Notification API to intercept notifications (.addInitScript page "(() => { const originalNotification = window.Notification; window.Notification = function(title, options) { console.log('Notification intercepted:', { title, options }); // You can store these notifications or perform assertions here // Optionally, call the original Notification constructor // return new originalNotification(title, options); }; window.Notification.permission = 'granted'; window.Notification.requestPermission = () => Promise.resolve('granted'); })()") ;; Navigate to the target website (.navigate page "https://yourwebsite.com") ;; Perform actions that trigger notifications ;; For example, clicking a button that sends a notification ;; (.click page "button#notify") ;; Wait for some time to allow notifications to be intercepted (Thread/sleep 5000) ;; Close the browser (.close browser-instance)))) ;; To run the function (enable-notifications) ‘’’

Hi can anyone help me, I am trying to accept a browser alert for notifications, it's a browser notification after I click accept on the website, is there a way to click "ok" on the address bar notifications in etaoin

@unison1978, are you asking the same question as @sasha_bogdanov_dev above?

I don't have access to the server backend I want to accept the notification using etaoin in clojure after the notification pops up, how do I click the accept the alert... I have tried accept-alert clj multimethod but I am not sure what arguments it accepts or how the multimethod works as the docstring is lacking in instruction

does accept-alert only deal with js in the HTML and not browser level notifications?

Have you tried this is code?

window.Notification.permission = 'granted';
                         window.Notification.requestPermission = () => Promise.resolve('granted');

That’s to grant permission

okay thanks, so it can't be done in etaoin and must be done using the js interop?

And you can overload Notification constructor:

window.Notification = function(title, options) {
                           console.log('Notification intercepted:', { title, options });
                           // You can store these notifications or perform assertions here
                           // Optionally, call the original Notification constructor
                           // return new originalNotification(title, options);
                         };
                         
And you can print it to console and read logs there. Or even you can “click” it if options contains .onClick field. Just call function inside

It could be run in Etaoin usuing e/js-execute

brilliant thank you mate!

No problem! Tell me if grant permissions work from JS. I am not fully sure

its a preexisting notification that has already popped up that I want to control but I don't know how, can you please help me to find it in the window object so I can send "granted" to it so it accepts the notification programmatically on behalf of the user

If you are talking about browser-level notifications then unfortunately you can’t. It is not in DOM

oh okay, yes it was browser-level... thanks for all your help though

But I can’t understand how you are interacting page where already that notification is… are you connecting to existing webdriver instance?

yes I have been automating the login procedure and clicking on the dom alerts for location and notification permissions but it also has a second layer of security in the way of browser notification acceptance

This is about it. I am not near the laptop to tell you more. Try to dig in permissions

Have you tried this is code?

window.Notification.permission = 'granted';
                         window.Notification.requestPermission = () => Promise.resolve('granted');
This way

I tried it and it's possible it could have worked but the window didn't disappear so I don't think there is a watch or fully connected state machine to trigger events but not sure tbh

the promise came back resolved though, just not sure how it would effect any other promises that are not resolved if there were any

I wish you luck in your search. It would be nice if you tell here the results

I think we Clojurians need to create our own browser that is fully connected and that uses Clojure as the language of choice so we are not flying blind, will try and keep this post up to date if I resolve it...

Are you guys tyring to get the browser to stop prompting for permissions? Ex:

I am talking about it, yes

If so, I poked around and found this works:

(require '[etaoin.api :as e])

(def driver (e/chrome {:prefs {"profile.default_content_setting_values.notifications" 1}}))

;; found this test page to play on
(e/go driver "")

;; turn on notifications on test page
(e/click driver {:type :checkbox})  

;; make test button visible
(e/scroll-query driver {:tag :button})

;; push test button to generate a notification
(e/click driver {:tag :button})

(e/quit driver)

It could be a helpful workaround but my initial task was trying to click the allow button on a notification that is presented from a third-party website programmatically

Not sure if you can do that with a WebDriver. I think the above prompt I showed is coming from the browser, and not the website.

I'm trying to automate the process of the user clicking on accept but if it can't be done then its not a massive deal I will just need to prompt my userbase to click them manually as and when they pop up

thanks for trying to help though

Once again! Gain permissions on start of driver instance

with this : {:prefs {"profile.default_content_setting_values.notifications" 1}}

👍 1

Yes, that's the only way I found. Did the above code help you @sasha_bogdanov_dev?

going to have to try it when my laptop recharges

Let us know how it goes!

yes! it worked! Nice one my friend

👍 1
🎉 1

I am not on my laptop. But I don’t see why it will not)