3

I'm a bit new to working with Selenium. I'm trying to trigger calls to Python when I do certain things in a Selenium controlled web browser.

For example, I'm running this script in Python 3.6.3 in Spyder using the run button.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from selenium import webdriver
b = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')

from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener

class EventListeners(AbstractEventListener):
    def before_navigate_to(self, url, driver):
        print("before_navigate_to")

    def after_navigate_to(self, url, driver):
        print("after_navigate_to")

d = EventFiringWebDriver(b,EventListeners())

d.get('https://www.cnn.com')

When I navigate in the browser by clicking a link or changing the url manually I don't see the events being fired. However I can verify that the url has changed. Also, if I call the get method from Python again, the events fire. This leads me to believe that these event listeners are only listening to the Python webdriver, and not to the actual browser. How do I get the Python code to run in response to browser actions such as navigation? Eventually I'd like to get Python running in response to javascript function executions as well ...

4
  • Python doesn't really have events. You need to switch to Javascript for this. Commented Aug 18, 2019 at 6:08
  • 1
    a javascript solution, that calls Python, would be acceptable, although I wanted to avoid RPCs Commented Aug 20, 2019 at 11:53
  • @pguardiario how to do this in JavaScript? (But with the same constraint: JavaScript runs outside the browser) Commented Jul 18, 2023 at 7:29
  • @Dims puppeteer has page.on which gets events when a request or response happens for example Commented Jul 19, 2023 at 1:19

1 Answer 1

2

Selenium is designed for automation testing. Since your task include manual intervention, it is difficult to (to the best of my knowledge) to implement what you need.

A workaround can (both with automated clicks and manual clicks) can be following:
Track navigation using mouse click tracking and grab the current URL information.

from selenium import webdriver
import time
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener
from pynput.mouse import Listener

b = webdriver.Chrome(executable_path=r'C:\Program Files\chromewebdriver\chromedriver.exe')
b.maximize_window()

class EventListeners(AbstractEventListener):
    def before_navigate_to(self, url, driver):
        print("before_navigate_to %s" % url)

    def after_navigate_to(self, url, driver):
        print("after_navigate_to %s" % url)

    def before_click(self, element, driver):
        print("before_click %s" % element)

    def after_click(self, element, driver):
        print("after_click %s" %element)

    def after_navigate_forward(self, driver):
        print("after_navigate_forward");

    def before_navigate_forward(self, driver):
        print("before_navigate_forward")

    def after_navigate_back(self, driver):
        print("after_navigate_back")

    def before_navigate_back(self, driver):
        print("before_navigate_back")

    def before_change_value_of(self, element, driver):
        print("before_change_value_of")

d = EventFiringWebDriver(b,EventListeners())

d.get('https://www.cnn.com')
d.implicitly_wait(20)
d.get('https://www.google.de')
d.implicitly_wait(20)
d.back()

def on_click(x, y, button, pressed):
    if pressed:
        print('Mouse clicked')
        time.sleep(2)
        print("Navigation to: %s " % b.current_url)

with Listener(on_click=on_click) as listener:
    listener.join()






This will result in the following output:
enter image description here
The first 6 lines of output have resulted from event listeners and last 5 lines have resulted from mouse click listener.
Note: All mouse clicks are tracked (not just this session clicks). Please remember to stop the program once you are done.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. It's not the way I wanted to solve the problem but it seems like it might be the way I have to solve the problem.
I'm afraid these events do not work so well in python. This is only a workaround though.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.