Python-Selenium-Chrome minimized window is maximizing itself - python

I am using Selenium with chromedriver in Python for testing 2 functionalities in parallel. Basically my webpage have 2 dropdown menus. Choosing an option from any dropdown menu will download a file. I have 2 scripts testing both dropdown menus separately. Now let say script-1 opens chrome and load the webpage. Now script-1 opens the dropdown-1 and it is in process of selecting an option from it as per the given input. During this if I navigate away from opened webpage to any other window eg. file explorer, the dropdown will get close without choosing the option and test case hangs. Also if I minimize the window before the dropdown is opened, the window will reopen itself when option is clicked for downloading the file. How can I stop the window from re-opening. Is there any Chrome setting for this?
Even if I move away from computer during execution, I want to run both the scripts in parallel side by side as they both open separate windows but after sometime one of them hangs the other window and I have to manually click on dropdown to bring it back to life. I am planning to run the scripts using multithreading with 2 threads referring to each of the scripts.
Note : I cannot use urlretrieve as there is no pre-defined URL for the files in webpage. PhantomJS do not support file download.
I am using Windows 10 machine.

There are several ways to do this
Run the parallel test using selenium grid and multiprocess, you could do this easily in docker, using docker/selenium, reference for using docker in https://github.com/SeleniumHQ/docker-selenium
Disable save dialog in chrome
options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {
"download.default_directory": r"yourpath",
"download.prompt_for_download": False
})
DesiredCapabilities caps = DesiredCapabilities.chrome();
driver = webdriver.Chrome(chrome_options=options)

Related

Firefox browser wont open maximized and wont switch tabs using Selenium

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.

How to open a new window on click with python, selenium, pyvirtualdisplay?

I wrote a scraper in python which uses selenium, chrome and an added extension. The scraper works locally with the display visible but not when I try to run it in headless mode.
On the scraped website there is a button which automatically opens a new window (I cannot access it via URL in order to open it in a new tab).
click_button() # the button opens a new window with a confirmation button
driver.switch_to.window(driver.window_handles[1]) # switching to the confirmation window
click_confirm() # when confirm is clicked the window closes automatically
driver.switch_to.window(driver.window_handles[0]) # switching back to original window
Chrome doesn't work in headless when it has extensions installed so I'm forced to use pyvirtualdisplay.
The problem is that the Display of pyvirtualdisplay opens only one browser window, therefore the confirmation window never appears. How can I allow pyvirtualdisplay to open new windows and alternate between them?
Turned out that pyvirtualdisplay did open a window, however its size was 1px by 1px. Apparently setting up the webdriver window size the following way doesn't work properly with pyvirtualdisplay:
options = Options()
options.add_argument('--window-size="1980,1080"')
driver = webdriver.Chrome(executable_path=chrome_driver_path, options=options)
Setting it up like bellow solved my problem and the window was of a normal size:
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.set_window_size(1980,1080)

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, e.g. different ones for Windows, Mac or Linux.

Using Winium (+ Selenium) with python only allows me to open the app, but then won't interact with it?

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.

Preventing Python webbrowser.open() from dominating screen and preventing other processes. Is it possible to open as minimised?

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!

Categories

Resources