How to avoid WebDriverException caused by Firefox updates - python

Several times I have faced this issue: I run my Python-Selenium script using Firefox, but instead of browser window, updates downloading pop-up appears and I get WebDriverException: "The browser appears to have exited ". Just after download completed browser window opens, but script is already stopped.. So how can I avoid script exit and force webdriver to wait until Firefox updates completion...

Disable auto update by passing custom FirefoxProfile when you open the
browser.
from selenium import webdriver
fp = webdriver.FirefoxProfile()
fp.set_preference('app.update.auto', False)
fp.set_preference('app.update.enabled', False)
browser = webdriver.Firefox(firefox_profile=fp)

Another solution is to disable autoupdate manually Options>Advanced>Update by "Never check for updates" radio btn. In my case its better idea because I have full control of compability between FF and WebDriver version then. Once I want to move to newer/newest version I do it via Help>About Firefox or if Im interested in specific version I can always download it here

Related

Python selenium webdriver remote wont opening new tab

i am having this issue where every time i do
driver.execute_script("arguments[0].click();", element)
or
driver.execute_script("window.open('_blank');")
browser blocks popup and prevents new tab from opening but when i use this
element.click()
It opens new tab without any issue so the problem is i am using webdriver.Remote for multilogin platform to automate some tasks so the execute_script method is very quick on the other hand element.click() takes forever.
What is the difference between them and for execute script new tab won't open i have tried doing this
options = webdriver.ChromeOptions()
options.set_capability("profile.default_content_settings.popups", 0)
driver = webdriver.Remote(link, options=options)
also added the same in experimental options as well as prefs but nothing works
thanks for your time any input is appreciated

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)

Edge Selenium shows the error: "Message: The specified window could not be found."

I use selenium with edge.
from selenium import webdriver
driver = webdriver.Edge()
driver.get("xxxxx")
When I opened an edge window,it will show
selenium.common.exceptions.NoSuchWindowException: Message: The specified window could not be found. when I run this code.
In other words, if I run this code when there is no edge window,it run well without exception.
How could I run selenium with edge when I has opened a edge window?
About Edge version:
Microsoft Edge 44.18362.449.0
Microsoft EdgeHTML 18.18362
from selenium import webdriver
driver = webdriver.Edge()
driver.get("xxxxx")
I have tested above code, but can't reproduce the problem. As far as I know, when we using selenium Edge webdriver with the legacy version Edge (Microsoft Edge 44.18362.449.0), it will auto close the existing Edge instance.
Please check your code, whether you want to switch handle to another window? Generally, this exception will throw when window target to be switched doesn’t exist, you could check this article.
Besides, from your description, the code runs well without exception if there is no Edge window. I suppose perhaps there have some process which is doing the automation test with the existing Edge browser window. Or, the existing Edge browser window is opened by the selenium Edge web driver, and it didn't close, so, it will show this exception.
To solve this issue, we need to close the Edge browser window or Edge browser process(from the Task Manager).
Besides, I suggest you could try to refer the following code, use try statement to catch the exception and remember to add close() and quit() methods to close the close the browser session at the end of automation test.
import time
from selenium import webdriver
print("*******************")
try:
driver = webdriver.Edge();
driver.get("https://www.bing.com")
time.sleep(5)
driver.close()
driver.quit()
except NoSuchWindowException:
print("Window disappeared, seems like it was closed manually")
print("*******************")
In our case, we closed open Edge browsers and re-ran the test. This worked!

Where is the problem about selenium with python?

When I run the code it takes about 5-6 sec to execute, after than nothings happens. I get no error, but the code is simply not working. Please, help me fast I've got a project to do in a month.
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_headless()
assert opts.headless
browser = Firefox(options=opts)
browser.get("https://duckduckgo.com")
Sorry about not knowing enough about stackoverflow. I think I wrote everything to the code entry.
Your code is perfectly working for me.
opts.set_headless()
browser = Firefox(options=opts)
This operation will create an option which hide the browser, then you set this option to you recently created Firefox tab.
Once you open the duckduckgo url,
It takes a few seconds to you program to open Firefox, but... you reach the end of your program and you browser is HIDING
Check with top command and you'll see Firefox still running.
Try to execute the same code but without the opts.set_headless() line.
Good Luck !
Try not use the "headless option":
from selenium.webdriver import Firefox
browser = Firefox()
browser.get("https://duckduckgo.com")

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