[Python] Python switch focus back to terminal from web browser with driver selenium

Discussão em 'Python' iniciado por Stack, Abril 30, 2025.

  1. Stack

    Stack Membro Participativo

    I am using selenium webdriver to open a webpage and fill out the form.

    anyways In the middle of the code i need to provide input data in the terminal

    but the focus still stays in the webbrowser, so i need to move mouse manulally back to the terminal.

    is there anyway to switch focus back to terminal from the webbrowser?
    sample code :

    import time
    from selenium import webdriver

    driver = webdriver.Chrome() # Optional argument, if not specified will search path.
    driver.get('https://www.seleniumeasy.com/test/basic-first-form-demo.html')
    search_box = driver.find_element_by_id('user-message')
    search_box.send_keys('John Rambo')
    time.sleep(5) # Let the user actually see something!
    test=input('please provide a number 0-9: \n') # I'd like the focus to switch back to the terminal
    driver.quit()

    Continue reading...

Compartilhe esta Página