简体   繁体   中英

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

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()

Through Selenium - no. It's a library that communicates exclusively to a browser, through the WebDriver protocol. It can only send request to the browser, which may (or may not) execute them.

As such, it cannot communicate with anything outside it - the OS, and its app switching routines including.
For that you need some other library, that can either issue OS requests (switch app), or simulates keyboard/mouse inputs. But these are OS dependant, eg different ones for Windows, Mac or Linux.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM