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.
Related
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.
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 have firefox installed and geckodriver.exe in the same directory. The same code works in windows but when i try to use it in ubuntu i have the following error: 'geckodriver.exe' executable needs to be in PATH.
the code is the following:
import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
url = 'https://www.idealista.com/venta-viviendas/barcelona/eixample/la-dreta-de-l-eixample/?ordenado-por=fecha-publicacion-desc'
options = Options()
options.headless = False
driver = webdriver.Firefox(options=options, executable_path=r'geckodriver')
driver.get(url)
time.sleep(10)
i've tried using the same code without calling gecko with r: driver = webdriver.Firefox(options=options, executable_path='geckodriver')
You can use webdriverManager, it's up-to-date and no need binary driver extension.So it will never show the error like you are getting:geckodriver.exe' executable needs to be in PATH. You have to install selenium4 and pip install webdriver-manager
#selenium4
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.options import Options
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()),options=options)
url = 'https://www.idealista.com/venta-viviendas/barcelona/eixample/la-dreta-de-l-eixample/?ordenado-por=fecha-publicacion-desc'
options = Options()
options.headless = False
driver.get(url)
time.sleep(10)
webdriverManager
Using the complete path solved the problem
import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
url = 'https://www.idealista.com/venta-viviendas/barcelona/eixample/la-dreta-de-l-eixample/?ordenado-por=fecha-publicacion-desc'
options = Options()
options.headless = False
driver = webdriver.Firefox(options=options, executable_path='/home/kevin/Desktop/Inmosoft/geckodriver')
driver.get(url)
time.sleep(10)
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
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);