chromedriver not found error on heroku app - python

I want to use selenium on my heroku app. So I added following buildpacks.
Following is the python code which uses the selenium, chrome and chromedriver
from selenium import webdriver
# from selenium.webdriver.chrome.options import Options
import os
import pickle
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options as ChromeOptions
chrome_bin = os.environ.get('GOOGLE_CHROME_SHIM', None)
opts = ChromeOptions()
opts.binary_location = chrome_bin
driver = webdriver.Chrome(executable_path="chromedriver",
chrome_options=opts)
I am getting following error
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'
Please help me.

I am answering for anyone who has same problem. So the problem was when I was adding buildpacks then it was asking me to run "git push heroku master" to use buildpack in app but that was not working. You need to make change in code and the you should push code on git so that it app runs again it rebuild itself.

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.

Deadlock with the launch of chromedriver on Ubuntu 20.04 LTS (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.

Display print messages in console PyInstaller

I'm using PyInstaller to convert my Python file to .exe; however, I see that upon running the executable file, I don't see any print messages in the terminal which I use for my reference - it's just blank (no print messages).
Command I used: pyinstaller --onefile -w myfile.py
I use Selenium module in my Python file, which uses chromedriver.exe, so after running the generated .exe file, this is what it shows:
Code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
from selenium.common.exceptions import TimeoutException
from time import sleep
import random
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
print("Opened the browser")
Pictures:
I need to see the print messages. Tried searching for answers but couldn't find any solutions!
How do I resolve this?
If you run your .exe from a terminal like PowerShell it will probably show the output you're looking for. The window in the screenshot looks like it is coming from the Selenium driver.
If you want this to work when double-clicking on the .exe, rebuild without the -w flag:
-w, --windowed, --noconsole
Windows and Mac OS X: do not provide a console window for standard i/o. On Mac OS this also triggers building a Mac OS .app bundle. On Windows this option is automatically set if the first script is a ‘.pyw’ file. This option is ignored on *NIX systems.

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}

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