-1

Is there any packages or ways to detect what is being clicking in a web browser? I mean get tag/xpath from the web browser (of what is being clicked)? To afterwards find it via selenium or similar?

Or even with to determine what it is with the coordinates of the mouse click.

Like the codegen in Playwright or similar, like a form of listening.

Hope this makes sense.

sdoedos
  • 41
  • 11

1 Answers1

1

We could add window listener with JavaScript using driver.execute_script to listen to any clicks, and then call function xpath as provided in SO answer to generate Xpath of an element. As a gist, below is the window.addEventListener script which handles any click event by displaying an alert with the clicked element text (if present) and its Xpath:

window.addEventListener('click', function(event) {alert(event.target.text+'=>'+xpath(event.target));})

And here is the relevant code to launch the browser, execute the script and sleep for 20 seconds to allow interaction on the browser:

def launch_url(url):
    driver = webdriver.Chrome('./chromedriver')
    driver.get(url)
    driver.execute_script('''
        function xpath(el) { 
            if (typeof el == 'string') return document.evaluate(el, document, null, 0, null); 
            if (!el || el.nodeType != 1) return ''; 
            if (el.id) return '//*[@id="' + el.id + '"'; 
            var sames = [].filter.call(el.parentNode.children, function (x) { return x.tagName == el.tagName }); 
            return xpath(el.parentNode) + '/' + el.tagName.toLowerCase() + (sames.length > 1 ? '['+([].indexOf.call(sames, el)+1)+']' : '')
        } 
        window.addEventListener('click', function(event) {alert(event.target.id+'=>'+xpath(event.target));})
    ''')
    time.sleep(20)

As a test, launched the SO main questions page with launch_url("https://stackoverflow.com/questions") and clicked on the "Ask Question" button:

enter image description here

Heelara
  • 861
  • 9
  • 17
  • 1
    This was exactly what I was looking for, thanks a lot! I have no idea, that you could do this! – sdoedos Nov 28 '22 at 08:22
  • Is there a way to catch the value of the xpath in this case? I've tried returning it instead of doing an alert. But it keeps giving me None – sdoedos Nov 28 '22 at 10:35
  • 1
    Even though there is an option to return values from a synchronous function with keyword `return`, there is no such option from an asynchronous call per [this SO thread](https://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) as the caller no longer listening. – Heelara Nov 29 '22 at 00:17