pythongoogle-chromeselenium-webdriverselenium-chromedrivergoogle-chrome-headless

Show cursor in Selenium Webdriver and s


I am building a Selenium Webdriver script in Python. My issue is that I am performing some actions that consist on moving to a certain position and clicking there. My action is not working, so I wanted to run the script as non headless to see what is happening, but, as the cursor does not appear in the screen, I am not able to see if my mouse is moving as I expected. Is there any way to enable seeing the cursor when running Selenium in a browser window? I am using Chromium but I am planning on adding more browsers to this script, so I would need something that at least works for Chromium, but it would be useful to have a solution for other browsers too. The code I have to move to the element and click it is this:

def scrollToCenter(driver, element):
    # Get the height of the window
    window_height = driver.execute_script("return window.innerHeight;")
    # Get the position of the element relative to the top of the page
    element_position = element.location['y']
    # Calculate the position to scroll to
    scroll_position = element_position - window_height/2
    # Scroll the page to the desired position
    driver.execute_script("window.scrollTo(0, {});".format(scroll_position))
    # move the cursor to the element
    actions = ActionChains(driver)
    actions.move_to_element(element).perform()

def simulateClick(driver, el):
    # Scroll to the point where the element is located
    driver.execute_script("arguments[0].scrollIntoView();", el)
    try:
        # Move to the element and click it
        scrollToCenter(driver, el)
        el.click()
    except Exception as e:
        print(e)

Seeing the scroll being performed would also be useful.

Also, I'd like to know if I can store this test case's headless execution in a video, so that I do not need to watch the execution while it happens to check if the mouse is in the correct position or not.


Solution

  • Part of the answer you're looking for can be found here: https://stackoverflow.com/a/68556892/7058266 (right-clicking makes a context menu appear at the point where your cursor clicked).

    However, if you have an issue related to clicks not working correctly in Chrome's default headless mode, then you should try the newer Chrome headless mode, which functions exactly like regular Chrome. Details here: https://stackoverflow.com/a/73840130/7058266

    And if you want to see the screen while running in headless mode, then you should check out Chrome's remote-debugging feature. Details here: Chrome remote debugging from another machine