Deadlock with the launch of chromedriver on Ubuntu 20.04 LTS (python) - python

I've trying to use seelnium chromedriver on Ubuntu 20.04 LTS (everything going well on work machine) for telegram bot.
Here is my imports and configs:
import selenium.webdriver.chrome.options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from xvfbwrapper import Xvfb
from webdriver_manager.chrome import ChromeDriverManager
import config
path = config.PATH #reading from different file (config) where PATH=r'/usr/bin/chromedriver'
Options = selenium.webdriver.chrome.options.Options()
Options.add_argument("--no--sandbox")
Options.add_argument('--headless')
Options.add_argument('log-level=3')
Options.add_argument("--remote-debugging-port=9222")
Here is info from server console:
whereis chromedriver > chromedriver: /usr/bin/chromedriver
chromedriver --version > ChromeDriver 104.0.5112.79 (3cf3e8c8a07d104b9e1260c910efb8f383285dc5-refs/branch-heads/5112#{#1307})
google-chrome --version > Google Chrome 104.0.5112.101
Here is part of code regarding selenium work:
#dp.message_handler(commands=['useragent'])
async def command_useragent(message: types.Message):
if message.from_user.id in admin:
Options.add_argument(f'user-agent={random.choice(config.user_agents_list)}')
vdisplay = Xvfb(width=1280, height=740, colordepth=16)
vdisplay.start()
time.sleep(10)
browser = webdriver.Chrome(service=Service(rf'{config.PATH}'), options=Options)
browser.get('https://google.com')
vdisplay.stop()
else:
pass
somewhy following string of code doesnt work:
browser = webdriver.Chrome(service=Service(rf'{config.PATH}'),options=Options)
I've tried to delete, change executable path, use or not use options "--no--sandbox", '--headless', "--remote-debugging-port=9222", use or not use pyvirtualdisplay and xvfbwrapper. I've absolutely confused how it should work.
here is part of error message regarding this code:
selenium.common.exceptions.WebDriverException: Message: unknown error:
Chrome failed to start: exited abnormally. (chrome not reachable)
(The process started from chrome location
/snap/chromium/2076/usr/lib/chromium-browser/chrome is no longer
running, so ChromeDriver is assuming that Chrome has crashed.)
Would anyone know how to fix this? Sorry if this is stupid question - I'm new in it. Thank you for advices.

Related

Selenium/chrome driver keeps crashing "Chrome failed to start: exited normally" and "DevToolsActivePort file doesn't exist"

I am trying to run my script for selenium/chromedrive but keep getting the error below.
Selenium ver 4.72
Chrome Browser version:Version 108.0.5359.125 (Official Build) (64-bit)
ChromeDriver version: ChromeDriver 108.0.5359.71
Message: unknown error: Chrome failed to start: exited normally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location C:/Program Files/Google/Chrome/Application/chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Script:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.binary_location = "C:/Program Files/Google/Chrome/Application/chrome.exe"
options.add_argument("--no-sandbox")
s = Service(executable_path=r'C:/Bin/chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.walmart.com")
Thanks you for any help
I have also tried Selenium manager but no good. I'm at my wits end
I think it might be because my chrome is installed in Application folder instead of user/appdata? Not too sure. What is the default location for Chrome? I've tried uninstialling chrome/and appdata and reinistalling but it puts me back at that folder.
There appears to be a bug with the latest chromedriver according to this thread: https://groups.google.com/g/chromedriver-users/c/wYA8JQB7l0A
Try running in headless mode and it will spin up properly.
Same here - Bug still there in chrome/chromedriver V110.XXXX # 14 Feb2 023
Disable the option --headless, that seems to work.

Chromedriver for linux32 does not exist - Python Selenium Chromedriver

I'm creating a cross platform python script that executes some commands with selenium.
I have two questions:
How come the following script works on windows but doesn't work on Raspberry pi OS 32bit? The only way this works is to remove the webdriver-manager, but this requires
manual installation of the webdriver.
I'm using a raspberry pi 3
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.utils import ChromeType
options = Options()
options.headless = True
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()), options=options)
driver.get("http://www.google.com")
print(driver.title)
The output is:
pi#raspberrypi:~/Documents/Software $ /bin/python /home/pi/Documents/Software/test.py
====== WebDriver manager ======
Current chromium version is 95.0.4638
Get LATEST chromedriver version for 95.0.4638 chromium
There is no [linux32] chromedriver for browser in cache
Trying to download new driver from https://chromedriver.storage.googleapis.com/95.0.4638.69/chromedriver_linux32.zip
Traceback (most recent call last):
File "/home/pi/Documents/Software/test.py", line 10, in <module>
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()), options=options)
File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/chrome.py", line 32, in install
driver_path = self._get_driver_path(self.driver)
File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/manager.py", line 30, in _get_driver_path
file = download_file(driver.get_url(), driver.ssl_verify)
File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/utils.py", line 98, in download_file
validate_response(response)
File "/home/pi/.local/lib/python3.9/site-packages/webdriver_manager/utils.py", line 80, in validate_response
raise ValueError("There is no such driver by url {}".format(resp.url))
ValueError: There is no such driver by url https://chromedriver.storage.googleapis.com/95.0.4638.69/chromedriver_linux32.zip
How can I create a python script that uses selenium webdriver in headless mode and works on every platform? I mean, if I use chromewebdriver in the script, the user who will use the script must have chrome installed, as well as if a firefox the user must have firefox installed. Is there any webdriver that works without external script installations?
Thanks
EDIT:
The problem is not with the webdriver manager but the fact that chromedrivers for chromium do not exist for linux32. In fact at the address: "https://chromedriver.storage.googleapis.com/95.0.4638.69/chromedriver_linux32.zip" there is no chromedriver, but replacing linux32 with linux64 a package is downloaded but not compatible with linux32.
The thing I don't understand is if the chromedrivers for linux32 don't exist then why installing them with: "sudo apt-get install chromium-chromedriver" and then removing the webdriver-manager calls from the code, does the python script work? Then there are chromedrivers for linux32, only they are not present in the main chromedriver site.
I am using a raspberry pi 3 with chromium 95.0.4638.69.
As there is no linux32 chromedriver developped by the official team (e.g. https://chromedriver.storage.googleapis.com/index.html?path=99.0.4844.17/), you can't use webdriver_manager because it's looking at their repositories.
Yet a compatible chromedriver version is maintained by the Raspian team, so in order to make it works you need to :
Install chromedriver for raspberry. e.g. for Linux:
sudo apt-get install chromium-chromedriver
When you instantiate your driver, you need to tell him where to find this chromedriver. Plus, you should also state that you use Chromium instead of Chrome (if so):
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
# browser is Chromium instead of Chrome
options.BinaryLocation = "/usr/bin/chromium-browser"
# we use custom chromedriver for raspberry
driver_path = "/usr/bin/chromedriver"
driver = webdriver.Chrome(options=options, service=Service(driver_path))
Then you're good to go:
driver.get("https://stackoverflow.com/")
If you need a code that works for every platform, the best option I found so far is to distinguished raspberry pi from the other system:
import platform
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = Options()
# a few usefull options
options.add_argument("--disable-infobars")
options.add_argument("start-maximized")
options.add_argument("--disable-extensions")
options.add_argument("--headless") # if you want it headless
if platform.system() == "Linux" and platform.machine() == "armv7l":
# if raspi
options.BinaryLocation = ("/usr/bin/chromium-browser")
service = Service("/usr/bin/chromedriver")
else: # if not raspi and considering you're using Chrome
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(
service=service,
options=options
)
Considering your question about not having to install Chrome manually to make Selenium works, you could probably install Chrome through a script to be sure that the user has it.
It will also work for a Docker use, e.g. for a DockerFile with a Linux amd64 environment:
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy instal

How to auto download .exe files to project directory based on browser version using Webdriver Manager in Python Robotframework

I tried below code but its downloaded and save it the path we configured in pip list [2nd line of code]. Instead of auto download in my local machine, I want to download directly into project directory because of few restriction issues in my organisation. Can anyone provide suggestions on this?
pip install webdrivermanager
webdrivermanager firefox chrome --linkpath /usr/local/bin
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
def get_chromedriver_path():
driver_path = ChromeDriverManager().install()
print(driver_path)
return driver_path
Library chromedriversync.py
${chromedriver_path}= chromedriversync.Get Chromedriver Path
Create Webdriver chrome executable_path=${chromedriver_path}
Go to www.google.com
Finally I got the solution. Below query helped me to auto download .exe files of the respective browser version to Project Current directory.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from lib2to3.tests.support import driver
import os
import os.path
from os.path import curdir
def get_chromedriver_path():
ReturnPath = os.getcwd()
driver_path = ChromeDriverManager(path=ReturnPath).install()
print(driver_path)
return driver_path
Library browser
${driver}= browser.Get Chromedriver Path
log ${driver}
Create Webdriver Chrome executable_path=${driver} chrome_options=${options}

Can not connect to the Service /usr/local/bin/geckodriver in python3

My code:
#!/usr/bin/python3
from selenium import webdriver
driver=webdriver.Firefox(executable_path=r'/usr/local/bin/geckodriver')
driver.get('http://www.python.org')
produces the following error:
ERROR : Message: Can not connect to the Service /usr/local/bin/geckodriver
My settings:
Mozilla Firefox 81.0
OS => Parrot sec(linux)
Python 3.8.6
geckodriver 0.27.0
How can I fix this?
Move geckodriver file to /usr/bin and than change code as follow.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
Hope it helpful.
It works in my side.

python using selenium, error: chrome unexpectedly exited. Status code was: 0

This is not a repost of
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed with ChromeDriver and Selenium in Python
I am using Linux and creating a new profile is not an option. I want to load an existing profile (not create a new one) just like selenium gui can.
I am able to get chromium to function, but not google chrome. Chrome will open but will kick back an
selenium.common.exceptions.WebDriverException: Message: Service /opt/google/chrome/chrome unexpectedly exited. Status code was: 0
error.
I am trying to start google chrome with user directory access, so I can capture existing sessions.
code that fails:
option.add_argument("user-data-dir=/home/user/.config/google-chrome/Default/") #)PATH is path to your chrome profile
driver = webdriver.Chrome('/opt/google/chrome/chrome', options=option)
code that works but launches chromium and not google-chrome:
option.add_argument("user-data-dir=/home/user/snap/chromium/common/.cache/chromium/Default/") #)PATH is path to your>
driver = webdriver.Chrome('/snap/bin/chromium.chromedriver', options=option)
I'm sure I am using the correct executable
I'm pretty sure I have the correct chromedriver driver install
root#Inspiron-laptop:/home/user# pip3 install chromedriver-autoinstaller
Requirement already satisfied: chromedriver-autoinstaller in /usr/local/lib/python3.8/dist-packages (0.2.2)
Just using it incorrectly.
How do I launch google-chrome from within selenium while accessing cache directories?
I am on Ubuntu 20.04
UPDATE:
Full script:
#!/usr/bin/python3
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from seleniumbase import BaseCase
from selenium.webdriver.chrome.options import Options
import time
import random
minptime = 25
maxptime = 120
class MyweClass(BaseCase):
def method_a():
option = webdriver.ChromeOptions()
option.add_argument('--disable-notifications')
option.add_argument("--mute-audio")
option.add_argument("user-data-dir=/home/user/.config/google-chrome/Default/") #)PATH is path to your chrome profile
driver = webdriver.Chrome('/opt/google/chrome/chrome', options=option)
driver.get("https://world.com/myworld")
print(f'driver.command_executor._url: {driver.command_executor._url}')
print(f'driver.session_id: {driver.session_id}')
time.sleep(18)
return driver
driver = MyweClass.method_a()
UPDATE II:
Same error using
option.add_argument("user-data-dir=~/.config/google-chrome/Default/")
and
driver = webdriver.Chrome('/opt/google/chrome/google-chrome', options=option)
and
chmod -R 777 /home/user/.config
To ensure user was hitting cache directory as user.
Google chrome info:
Thumb rule
A common cause for Chrome to crash during startup is running Chrome as root user (administrator) on Linux. While it is possible to work around this issue by passing --no-sandbox flag when creating your WebDriver session, such a configuration is unsupported and highly discouraged. You need to configure your environment to run Chrome as a regular user instead.
This error message...
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.26.436382 (70eb799287ce4c2208441fc057053a5b07ceabac),platform=Linux 4.15.0-109-generic x86_64)
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.
As per your code trials seems you are trying to access a Chrome Profile so you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:\\path\\to\\your\\profile\\Google\\Chrome\\User Data\\Profile 2")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")
References
You can find a couple of detailed discussions in:
How to open a Chrome Profile through Python
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed with ChromeDriver and Selenium in Python
How to use Chrome Profile in Selenium Webdriver Python 3
Selenium: Point towards default Chrome session

Categories