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.
I am using sublime to code python scripts. The following code is for selenium in python to install the driver automatically by using the webdriver_manager package
# pip install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
#s=Service(path)
#driver=webdriver.Chrome(service=s)
driver.get('https://www.google.com')
driver.find_element(By.NAME, 'q').send_keys('Yasser Khalil')
The code works fine but I got a warning like that
Demo.py:7: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
driver = webdriver.Chrome(ChromeDriverManager().install())
How to fix such a bug?
This error message...
DeprecationWarning: executable_path has been deprecated, please pass in a Service object
...implies that the key executable_path will be deprecated in the upcoming releases.
This change is inline with the Selenium 4.0 Beta 1 changelog which mentions:
Deprecate all but Options and Service arguments in driver instantiation. (#9125,#9128)
Solution
With selenium4 as the key executable_path is deprecated you have to use an instance of the Service() class along with ChromeDriverManager().install() command as discussed below.
Pre-requisites
Ensure that:
Selenium is upgraded to v4.0.0
pip3 install -U selenium
Webdriver Manager for Python is installed
pip3 install webdriver-manager
You can find a detailed discussion on installing Webdriver Manager for Python in ModuleNotFoundError: No module named 'webdriver_manager' error even after installing webdrivermanager
Selenium v4 compatible Code Block
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")
Console Output:
[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 96.0.4664
[WDM] - Get LATEST driver version for 96.0.4664
[WDM] - Driver [C:\Users\Admin\.wdm\drivers\chromedriver\win32\96.0.4664.45\chromedriver.exe] found in cache
You can find a detailed discussion on installing Webdriver Manager for Python in Selenium ChromeDriver issue using Webdriver Manager for Python
Incase you want to pass the Options() object you can use:
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()
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get("https://www.google.com")
TL; DR
You can find the relevant Bug Report/Pull Request in:
Bug Report: deprecate all but Options and Service arguments in driver instantiation
Pull Request: deprecate all but Options and Service arguments in driver instantiation
This works for me
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
ser = Service(r"C:\chromedriver.exe")
op = webdriver.ChromeOptions()
s = webdriver.Chrome(service=ser, options=op)
Extending on the accepted answer, the Service class allows to explicitly specify a ChromeDriver executable in the same way as previously using the executable_path parameter. In this way existing code is easily migrated (clearly you need to replace C:\chromedriver.exe above by your path).
I could figure it out
# pip install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.maximize_window()
driver.get('https://www.google.com')
driver.find_element(By.NAME, 'q').send_keys('Yasser Khalil')
I found this deprecation issue is appearing on Selenium, Pip and Python updates. so simply just change :
before:
from selenium import webdriver
chrome_driver_path = 'C:/Users/Morteza/Documents/Dev/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver_path)
url = "https://www.google.com"
driver.get(url)
after:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
s=Service('C:/Users/Morteza/Documents/Dev/chromedriver.exe')
browser = webdriver.Chrome(service=s)
url='https://www.google.com'
browser.get(url)
All the above answers refer to Chrome, adding the one for Firefox
Install:
pip install webdriver-manager
Code:
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(service=Service(executable_path=GeckoDriverManager().install()))
Reference: https://github.com/SergeyPirogov/webdriver_manager/issues/262#issuecomment-955197860
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service_obj = Service("WebDrivers_path\chromedriver.exe")
driver = webdriver.Chrome(service=service_obj)
driver.get("https://www.google.com")
Simplest option with Chrome auto-installer:
from selenium import webdriver
import chromedriver_autoinstaller
from selenium.webdriver.chrome.service import Service
chromedriver_autoinstaller.install()
driver = webdriver.Chrome(service=Service())
Have a look at the new definition in the Service object here.
My solution
from selenium.webdriver.chrome.service import Service
chrome_executable = Service(executable_path='chromedriver.exe', log_path='NUL')
driver = webdriver.Chrome(service=chrome_executable)
if you are using any IDE like PyCharm install webdriver-manager package of that IDE as how do install for selenium package
You can create an instance of ChromeOptions, which has convenient methods for setting ChromeDriver-specific capabilities. You can then pass the ChromeOptions object into the ChromeDriver constructor:
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(options);
Since Selenium version 3.6.0, the ChromeOptions class in Java also implements the Capabilities interface, allowing you to specify other WebDriver capabilities not specific to ChromeDriver.
ChromeOptions options = new ChromeOptions();
// Add the WebDriver proxy capability.
Proxy proxy = new Proxy();
proxy.setHttpProxy("myhttpproxy:3337");
options.setCapability("proxy", proxy);
// Add a ChromeDriver-specific capability.
options.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(options);
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}
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.
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