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, e.g. different ones for Windows, Mac or Linux.
Related
I have a very specific need using selenium and I do not want to use pyautogui to do so. I need, at some moment of the program's execution, to open the inspect window( Developer's Window). I tried directly sending keys to the body element and using action chains and did not suceded in any. There is a way to automatically open the developer's window setting the options of the webdriver that worked, but there is no option to activate it at a certain point of the execution, making it always opened.
I would like to know if there is a way to send those key inputs without resorting to manual intervention.
I tried what I mentioned before, it did not work as intended
I'm opening Firefox browser through selenium(3.141.0) on python 3.9, and it will always start minimized, even though in code I passed function .maximise_window() after opening the browser. When its started this way, it wont execute code to maximise and it wont execute code to switch tabs, and it wont even do it if im trying to switch tabs manually with my mouse.
If I immediately click on the initiated firefox browser on my taskbar when the program is started, it will function normally, but it will open my browsers home tab, and execute my code in new tab. Thats why you may see in code a part that closes that first tab. When i dont do it and when it starts "faulty", the home tab wont be opened.
Im also using the lenght of tabs as an indicator if the browser initiated faulty, where I tried to put it into loop and make it restart till it opens with 2 tabs, but it just wont unless I manually click it.
The only solution so far I can think of is kinda "hacky"...using pyautogui to scan my taskbar after initializing browser and clicking it fast, but I dont really like the idea.
The code goes through my company data warehouse site and manipulates it to download data.
Update
Other hacky solution I found is starting 2 browsers. First won't work, but second will. Meaning that browser works normally IF there is another Firefox browser open at the time.
Snippets of code:
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
class DWH_browser:
def __init__(self):
self.browser = webdriver.Firefox()
self.browser.maximize_window()
self.browser.get("www.letskeepthecompanysiteasecret.org")
self.len_windows = len(self.browser.window_handles)
print(len(self.browser.window_handles))#this next part is used to close the extra tab when browser
#started normally and opens an extra tab
if len(self.browser.window_handles) == 2:
self.browser.switch_to.window(self.browser.window_handles[0])
self.browser.close()
self.browser.switch_to.window(self.browser.window_handles[0])
self.a = ActionChains(self.browser)
time.sleep(5)
DWH = DWH_browser()#and i initiate it in the code "normally"
#This was the other code I tried using to initate the browser and restart till its in 2 tabs, but not working
# issue_lenght = 1
# while issue_lenght == 1:
# DWH = DWH_browser()
# issue_lenght = DWH.len_windows
# if DWH.len_windows == 1:
# DWH.browser.quit()
# print("RESTARTING BROWSER")
Summarizing your issues:
Always start minimized.
Passed function maximise_window() after opening the browser.
Won't execute code to switch tabs.
To get rid of all these issues you need to ensure that:
Selenium is upgraded to current levels Version 4.4.0.
GeckoDriver is updated to current GeckoDriver v0.31.0 level.
Firefox Browser is updated to current firefox=103.0.2.
Additionally, you won't be needing self.browser.maximize_window() as firefox by default opens in a maximized mode.
Finally, trying to switch tabs manually with my mouse is a big no as the program execution may get interupted.
When I try to run any kind of code using winium, it will open the app, but then won't execute any of the code afterwards. It's not as if it throws up an error, it just hangs there and won't move on.
I Am using Python 3.7 on a Windows 10 PC.
I have tried the two 'magic' examples that are listed on the github wiki page for Winium, but even that doesn't work. I am able to use selenium to do automated web testing, so I don't think the selenium module is the issue. I have tried importing the time module and making it sleep for 10 seconds in between lines but this has no effect on the outcome.
from selenium import webdriver
driver = webdriver.Remote(
command_executor='http://localhost:9999',
desired_capabilities={
"debugConnectToRunningApp": 'false',
"app": r"C:/windows/system32/calc.exe"
})
# THIS IS WHERE IT SEEMS TO PAUSE INDEFINITELY
window = driver.find_element_by_class_name('CalcFrame')
view_menu_item = window.find_element_by_id('MenuBar').find_element_by_name('View')
view_menu_item.click()
view_menu_item.find_element_by_name('Scientific').click()
view_menu_item.click()
view_menu_item.find_element_by_name('History').click()
window.find_element_by_id('132').click()
window.find_element_by_id('93').click()
window.find_element_by_id('134').click()
window.find_element_by_id('97').click()
window.find_element_by_id('138').click()
window.find_element_by_id('121').click()
driver.close()
I would expect it to press the corresponding buttons, but it doesn't seem to do anything except open the calculator app.
I think this example is written for an older version of calculator. In Windows 10, the "Scientific" button is under the Menu button.
You'll have to find the menu button, click it, and then look for the element "Scientific" in the list.
Also, the numeric values for your arithmatic case are not correct. Pick up a UI inspector tool (inspect.exe, uispy, etc...) to make sure you are targeting the elements correctly.
I am using webbroswer.open() in a loop to download multiple files at given intervals.
The issue I am having is that whenever the browser window opens, it becomes the primary window and thereby interrupts and disrupts my ability to use the computer. Downloading multiple files means this can last some time. The broswer continuously flashing open is obviously jarring.
Is there any way to instruct webbrowser to open the browser minimised by default or otherwise avoid this issue in some other ingenious way?
Much appreciated!
If you are open to using other modules I would urge you to look into selenium. This allows you to do many things, and one of them is to launch in headless mode (so as not to disturb you as it loads pages). The documentation is at:
https://selenium-python.readthedocs.io/
And you would be interested in the headless option
You would be advised though to make sure your script works without this enabled before you enable it though.
Sample code:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
my_options = Options()
my_options.headless = True # set to False for debugging!!
browser = webdriver.Chrome(options=my_options)
browser.get('http://www.google.com')
print('Done.')
You will need to download the proper drivers (just follow the instructions on the link I posted) for whatever browser you'd like. I picked Chrome, but they have Edge, Firefox, and Safari browsers as well!
I'm using Selenium to scrape data from a website. The website requires window focus in order to display certain elements that I need.
I want to be able to run my program in the background, without having to focus the window while it's running.
Is there any way to trick the site into thinking it's focused on?
I'm using the selenium chrome driver.
Edit: Here's a quick and dirty test I built.
Check out the code on GitHub
The website background color will turn black when the window.onblur event is recieved, and turn back white when the window.onfocus event is recieved.
I want to fake those events, to make the browser think it's recieved a focus event.
Since the page gets focus information through the onfocus and onblur callbacks in the window object, you can just call them yourself.
browser.execute_script("window.onfocus()")
browser.execute_script("window.onblur()")
For fun, try this script out:
from selenium import webdriver
import time
chromedriver = "./chromedriver"
browser = webdriver.Chrome(executable_path = chromedriver)
browser.get('http://anubiann00b.github.io/FocusTest/')
while True:
browser.execute_script("window.onfocus()")
time.sleep(1)
browser.execute_script("window.onblur()")
time.sleep(1)
And of course, if you want to make the browser think it's always focused, make the onblur method call the onfocus method:
browser.execute_script("window.onblur = function() { window.onfocus() }")