Search code examples
pythonseleniumfirefoxscreen-scraping

Visual feedback for Selenium clicks


I'm using Selenium (Python) with the Firefox webdriver to scrape a website. While navigating through the website I'd like to actually see where it clicks. Something like a circle which appears for 0.5 sec after the click.

Answers for other languages (Java) are also welcome. As long as it's Selenium.

Does anyone have experience with this?


Solution

  • You can use such function when you are clicking on DOM elements.

    def change_style(elem, driver, new_style):
        driver.execute_script("arguments[0].{} = arguments[1]".format('style'), elem, new_style)
    

    And then call this function like this:

    elem = driver.find_element... #your code to get the element
    old_style = elem.get_attribute('style') #save the original style before you change it
    highlight_style = "background: yellow; border: 2px solid red;" #change the bg of element to yellow and add a red border to it
    change_style(elem, driver, highlight_style)
    ...
    #your code to click the element, and when clicking next item you can change the last item back to its original style
    change_style(elem, driver, old_style)
    

    So between two clicks, you can see the highlighted web element which was clicked last. Hope it helps.