Chrome closes while/before completely downloading file using Selenium - python

I have confirmed that my code works on Windows and was working on Arch a few months back (deleted my Arch VM, so can't test right now). My original code selects data and downloads a file from a site that requires login credentials, so cant paste code referencing those links. Code worked fine on other OSes, but I cant get it to work in my Ubuntu VM that I just created. I tested on the python.org website to download the latest python .tgz file and having the same results, works on Windows, but not on the VM. Once the link is clicked, it begins to download the file. After the download begins, Chrome closes within a second or so and kills the download leaving it partially downloaded and unusable. Very small files seem to complete downloading before Chrome exits, but not larger ones. No errors are shown and the code completes successfully in VS Code.
I also tested having the Chrome Driver open the website and I would manually click the link. Chrome would shutdown and partially download the file the same as having my code perform the actions.
Has anyone else experienced this before? Is it an issue with Ubuntu?
Here is my current software/packages installed -
selenium 4.1.0,
webdriver_manager 3.5.2,
VS Code 1.64.2,
Python 3.10.2,
Ubuntu 20.04.3 LTS on VMWare
Here is the code
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
py_website = 'https://www.python.org/downloads/release/python-3102/'
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), chrome_options=options)
driver.get(py_website)
driver.find_element_by_xpath("//a[#href='https://www.python.org/ftp/python/3.10.2/Python-3.10.2.tgz']").click()
Edit 2/16/2022 12:50pm CST
Tested on a new Mint VM and had to add the following code for Chrome to launch. Seems to be working on Mint, but Ubuntu still having the same issue with the added arguments.
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
With out this code, I received the following error on Mint.
WebDriverException: Message: unknown error: Chrome failed to start: crashed.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Edit 2/16/2022 21:30pm CST
Tried using PyCharm and it is working flawlessly. Must be something with my settings in VS Code.

Related

Unable to open chromium browser in VPS with Selenium

I need to open chromium or chrome with selenium in Ubuntu 22 deployed in Digital Ocean VPS droplet. The issue is it doesnt open a chrome tab, the chromium and chromedriver are same version.This works fine in my localhost, with ubuntu 22.However, I was not able to run it when deployed in vps
options = webdriver.ChromeOptions()
options.add_extension(pluginfile)
driver = webdriver.Chrome('chromedriver', options=options)
I tried several things including the suggestions here Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed
The error is
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /snap/bin/chromium is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
As I already mentioned, this will work perfectly fine in localhost with same OS and chromedriver and chromium version.
Any idea how to make it work in a VPS?
After several attempt, I finally found the reason
VPS doesn't allow selenium to open the browser.It only works in headless mode, which doesn't fit my requirements since I have plugins and headless mode will not run with plugins

Website is not opening in selenium webdriver of google chrome using python

I am trying to open the website in selenium python but it is showing blank page. but when i open that website in normal google chrome it is working .
here is the code i am writing to open the website.
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://shop.coles.com.au/a/wentworth-point/home")
Error I am Getting in chrome console Failed to load resource: the server responded with a status of 429 () and Failed to load resource: net::ERR_UNKNOWN_URL_SCHEME –
You must specify the chromedriver path. Something like this:
https://seleniumbyexamples.github.io/navget
Verify that version of chrome driver is same of chrome browser installed on your machine
and make sure that path of chrome driver is set in your PATH variable.
http://chromedriver.chromium.org/downloads
The error code 429 that you are getting means too many requests are sent in a given amount time. Just try to clean your build history and run it again. Regarding the version of Chrome, if that was to be the issue the browser must have not opened in the first place and you would have got other exception like SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 83
Just to be sure you mat check for the version of chrome you are using and the driver downloaded in the system, if it is not appropriate download the latest chrome driver from web and provide in the executable path to chrome or else add it in environment variables of your system.

ChromeDriver Does Not Open Websites

I am experiencing a very strange behaviour when testing Chrome via selenium webdriver.
Instead of navigating to pages like one would expect the 'get' command leads only to the download of tiny files (no type or.apsx files) from the target site.
Importantly - this behavior only occurs when I pass chrome_options as an argument
to the Chrome driver.
The same testing scripts work flawless with firefox driver.
Code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options # tried with and without
proxy = '127.0.0.1:9951' # connects to a proxy application
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % proxy)
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('whatismyip.com')
Leads to the automatic download of a file called download (no file extension, Size 2 Byte).
While calling other sites results in the download of small aspx files.
This all happens while the browser page remains blank and no interaction with
elements happen = the site is not loaded at all.
No error message, except element not found is thrown.
This is really strange.
Additional info:
I run Debian Wheezy 32 bit and use Python 2.7.
Any suggestions how to solve this issue?
I tried your code and captured the traffic on localhost using an SOCKS v5 proxy through SSH. It is definitely sending data through the proxy but no data is coming back. I have confirmed the proxy was working using Firefox.
I'm running Google Chrome on Ubuntu 14.04 LTS 64-bit. My Chrome browser gives me the following message when I try to configure a proxy in its settings menu:
When running Google Chrome under a supported desktop environment, the
system proxy settings will be used. However, either your system is not
supported or there was a problem launching your system configuration.
But you can still configure via the command line. Please see man
google-chrome-stable for more information on flags and environment
variables.
Unfortunately I don't have a man page for google-chrome-stable.
I also discovered that according to the selenium documentation Chrome is using the system wide proxy settings and according to their documentation it is unknown how to set the proxy in Chrome programmatically: http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#using-a-proxy

Does Chrome Web Drivers needs to be in Client's System while using Selenium

I have tried searching the official documentation, but no result. Does Chrome Web Driver needs to be in client's system for a python script using Selenium to run? I basically want to distribute compiled or executable file versions of the application to the end user. How do i include Chrome Web driver with that package?
Yes you will have to download and put chromedriver to your system and then need to call in selenium code.
Download chromedriver from here : Chromedriver for selenium
Below code will help you to call chrome driver using selenium with python :
import os
from selenium import webdriver
chromedriver = "/Users/mike/Downloads/chromedriver" [you please put your actual path of chrome driver]
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
Hope this help you.
If you are using selenium RC , then you can set path like below :
selenium = new DefaultSelenium(Server, ServerPort, "*googlechrome", DomainURL);
chromedriver needs to be installed on the machine that will launch the instance of the Chrome browser.
If the machine that launches the Chrome instance is the same machine as where the Python script resides, then the answer to your question is "yes".
If the machine that launches the Chrome instance is a machine different from the machine that runs your Python script, then the answer to your question is "no".

Cannot create browser process when using selenium from python on RHEL5

I'm trying to use selenium from python but I'm having a problem running it on a RHEL5.5 server. I don't seem to be able to really start firefox.
from selenium import webdriver
b = webdriver.Firefox()
On my laptop with ubuntu this works fine and it starts a brings up a firefox window. When I log in to the server with ssh I can run firefox from the command line and get it displayed on my laptop. It is clearly firefox from the server since it has the RHEL5.5 home page.
When I run the python script above on the server it (or run it in ipython) the script hangs at webdriver.Firefox()
I have also tried
from selenium import webdriver
fb = webdriver.FirefoxProfile()
fb.native_events_enabled=True
b=webdriver.Firefox(fb)
Which also hangs on the final line there.
I'm using python2.7 installed in /opt/python2.7. In installed selenium with /opt/python2.7/pip-2.7.
I can see the firefox process on the server with top and it is using a lot of CPU. I can also see from /proc/#/environ that the DISPLAY is set to localhost:10.0 which seems right.
How can I get a browser started with selenium on RHEL5.5? How can I figure out why Firefox is not starting?
It looks like the problem I'm encountering is this selenium bug:
http://code.google.com/p/selenium/issues/detail?id=2852
I used the fix described in comment #9 http://code.google.com/p/selenium/issues/detail?id=2852#c9
That worked for me.

Categories