When running this code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdrivermanager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().download_and_install())
driver.get("http://www.python.org")
This results in the following exception at the line where the chromedriver is installed:
TypeError: expected str, bytes or os.PathLike object, not tuple
Note that I am aware that there already exist many threads about this topic but since the webdrivermanager seems to have been updated majorly the previous solutions do not work.
Also a quick side note: I installed webdrivermager via conda instead of pip. but that should not be of concern.
EDIT: Entire stack trace:
Traceback (most recent call last): File "C:\Users\stefa\OneDrive -
Johannes Kepler Universität
Linz\Dokumente\GitHub\briefly\src\crawler\crawler.py", line 19, in
driver = webdriver.Chrome(ChromeDriverManager().download_and_install()) File
"C:\Users\stefa\anaconda3\envs\briefly\lib\site-packages\selenium\webdriver\chrome\webdriver.py",
line 73, in init
self.service.start() File "C:\Users\stefa\anaconda3\envs\briefly\lib\site-packages\selenium\webdriver\common\service.py",
line 72, in start
self.process = subprocess.Popen(cmd, env=self.env, File "C:\Users\stefa\anaconda3\envs\briefly\lib\subprocess.py", line 951,
in init
self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\stefa\anaconda3\envs\briefly\lib\subprocess.py", line
1360, in _execute_child
args = list2cmdline(args) File "C:\Users\stefa\anaconda3\envs\briefly\lib\subprocess.py", line 565,
in list2cmdline
for arg in map(os.fsdecode, seq): File "C:\Users\stefa\anaconda3\envs\briefly\lib\os.py", line 822, in
fsdecode
filename = fspath(filename) # Does type-checking of filename. TypeError: expected str, bytes or os.PathLike object, not tuple
Install Webdriver Manager for Python:
pip install webdriver-manager
Import ChromeDriverManager:
from webdriver_manager.chrome import ChromeDriverManager
Use webdriver:
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
There are two issues in your code block as follows:
You need to import ChromeDriverManager from webdriver_manager.chrome
As per Webdriver Manager for Python download_and_install() isn't supported and you have to use install()
So your effective code block will be:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://www.python.org")
On windows-10 system the console output will be:
C:\Users\Admin\Desktop\Python Programs>python webdriver-manager_ChromeDriverManager.py
[WDM] -
[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 95.0.4638
[WDM] - Get LATEST driver version for 95.0.4638
[WDM] - There is no [win32] chromedriver for browser 95.0.4638 in cache
[WDM] - Get LATEST driver version for 95.0.4638
[WDM] - Trying to download new driver from https://chromedriver.storage.googleapis.com/95.0.4638.54/chromedriver_win32.zip
[WDM] - Driver has been saved in cache [C:\Users\Admin\.wdm\drivers\chromedriver\win32\95.0.4638.54]
DevTools listening on ws://127.0.0.1:50921/devtools/browser/c26df2aa-67aa-4264-b1dc-34d6148b9174
You can find a relevant detailed discussion in ModuleNotFoundError: No module named 'webdriver_manager' error even after installing webdrivermanager
Here my solution:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
s = Service('chromedriver/chromedriver96.exe')
driver = webdriver.Chrome(service=s, options=options)
Related
I'm refactoring my app from procedural code to OOP. I am trying to do this Driver class.
UPDATE: this works in Windows but not in Mac.
# IMPORTS
from sys import platform
import os
from os import system
from selenium import webdriver
from selenium.webdriver import Firefox, FirefoxOptions
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# import Action chains
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
from selenium.webdriver.firefox.service import Service
class Driver():
def __init__(self):
#set executable path to driver
self.dirname = os.path.dirname(__file__)
if platform == "win32":
self.executable_path = os.path.join(self.dirname, 'geckodriver.exe') #must save the gecko file to same directory where python is. path to geckodriver (firefox drive/motor) on your machine
print("Gecko (Firefox) filepath is: ", self.executable_path)
if platform == "darwin":
self.executable_path = os.path.join(self.dirname, 'geckodriver') #must save the gecko file to same directory where python is. path to geckodriver (firefox drive/motor) on your machine
print("Gecko (Firefox) filepath is: ", self.executable_path)
self.service = Service(self.executable_path)
self.opts = FirefoxOptions()
#self.opts.add_argument(f"--width={int(screen_width/4)}")
#self.opts.add_argument(f"--height={int(screen_height/2)}")
self.driver = Firefox(service=self.service, options=self.opts)
self.driver.set_window_position(-10, 0)
self.driver.get("https://google.com/")
Driver()
This will give me the following error:
Traceback (most recent call last):
File "/driverClass.py", line 72, in <module>
Driver()
File "/driverClass.py", line 66, in __init__
self.driver = Firefox(service=self.service, options=self.opts)
TypeError: __init__() got an unexpected keyword argument 'service'
Why is this? I'm refactoring my code to OOP. The code worked before when using procedural code.
This is from the working code:
# driver configs
service = Service(executable_path) #pass in path to geckodriver
opts = FirefoxOptions()
#opts.add_argument(f"--width={int(screen_width/4)}")
#opts.add_argument(f"--height={int(screen_height/2)}")
driver = Firefox(service=service, options=opts)
driver.set_window_position(-10, 0)
#driver.set_window_size(int(screen_width/4), int(screen_height))
driver.get("https://google.com/")
This error message...
TypeError: __init__() got an unexpected keyword argument 'service'
...implies that service is an unexpected keyword argument.
The possible reason, you are still using Selenium v3.x and the keyword argument service
wasn't supported.
Solution
Since Selenium 4.0 Beta 1:
Deprecate all but Options and Service arguments in driver instantiation. (#9125,#9128)
So you need to upgrade to Selenium 4.x
References
You can find a couple of relevant detailed discussion in:
TypeError: init() got an unexpected keyword argument 'service' error using Python Selenium ChromeDriver with company pac file
self.driver = Firefox(service=self.service, options=self.opts)
The Firefox webdriver doesn't take a service keyword argument:
https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.webdriver
It would be helpful to see the older working version.
I encountered the same error and fixed this problem by upgrading to Selenium 4.0
I am using Anaconda python.
To upgrade to Selenium 4.0 on Anaconda, run the following command;
$ conda install https://conda.anaconda.org/conda-forge/noarch::selenium
The following command also works fine.
$ conda install selenium --channel conda-forge/noarch
At this point of writing, you cannot upgrade Selenium using the normal conda command conda update selenium. Running this command will install selenium v3
You have to specify the installation on the noarch version which supports selenium v4.1
The link below teaches you how to do that.
Specify platform during package install
As you can see when you call
Firefox(service=self.service, options=self.opts)
it raises because Firefox class does not accept service kwarg
If you are trying to set up a Firefox web driver you can see similar question
im working on a project and have this as code:
import selenium
from selenium import webdriver
driver = webdriver.Chrome()
print(driver.current_url)
but i get an error and i dont know how to fix it, well mutle tracebacks to error i think, here is the error:
Traceback (most recent call last):
File "C:\Users\ytty\PycharmProjects\test hack\main.py", line 2, in <module>
from selenium import webdriver
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 36, in <module>
from .service import Service
File "C:\Users\ytty\PycharmProjects\test hack\venv\lib\site-packages\selenium\webdriver\firefox\service.py", line 21, in <module>
class Service(service.Service):
AttributeError: module 'selenium.webdriver.common.service' has no attribute 'Service'
Process finished with exit code 1
can anyone help?
I do it this way:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options, executable_path=ChromeDriverManager().install())
This way it will automatically download the optimal ChromeDriver version on your cache and use it every time you need. Also the experimental part helps to avoid some error that might appear due to driver bugs
I have a python script which use selenium and chromedriver.
It runs on my CentOS8 VPS perfectly for 3 days without any problem.
But since this morning, the script launched, wait almost 80 secondes and display this :
[12/Jan/2021 23:04:51] ERROR - Failed : Message: chrome not reachable
Traceback (most recent call last):
File "script.py", line 55, in <module>
driver = launch()
File "script.py", line 37, in launch
browser = webdriver.Chrome('/usr/bin/chromedriver',chrome_options=chrome_options)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
No modification have been made, why does it fail now ?
I don't have any screen on my VPS so I can't see more information.
Here is some info :
yum info on chromedriver :
Nom : chromedriver
Version : 87.0.4280.88
Publication : 1.el8
Architecture : x86_64
Taille : 27 M
Source : chromium-87.0.4280.88-1.el8.src.rpm
Dépôt : #System
Depuis le dé : epel
google-chrome --version :
Google Chrome 87.0.4280.141
Begin of the script :
from dotenv import load_dotenv
from logger import logger as l
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
import time
import sys
import subprocess
load_dotenv(verbose=True)
dotenv_path = '.env'
load_dotenv(dotenv_path)
def launch():
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
browser = webdriver.Chrome('/usr/bin/chromedriver',chrome_options=chrome_options)
l.info('Started Chrome')
return browser
Problem solved but don't understand how.
I just restart my VPS (reboot), and ... it works again.
Weird
EDIT : Find why !
I just made a mistake at the end of my script :
b.close();
But "b" don't exist, my driver variable name is "driver".
The exception was catched and not displayed, so I don't saw anything.
But today, I launch a "top" command, and see all "chrome" process running in background.
Probably after several days, memory was full, and Chrome can't launch.
The error was not clear but anyway, it was my fault.
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.
Solution
Remove the following chrome_options:
--no-sandbox
and execute your code as a non root user.
Outro
Here is the link to the Sandbox story.
I am using selenium-wire to click a button using Chrome browser. Up until today, my code worked fine. However, now I get the following error and I am not sure why:
Traceback (most recent call last):
File "Scraping_fx.py", line 1, in <module>
from seleniumwire import webdriver
File "C:\Users\me\Anaconda3\envs\project\lib\site-
packages\seleniumwire\webdriver\__init__.py", line 3, in <module>
from .browser import Chrome, Edge, Firefox, Safari # noqa
File "C:\Users\me\Anaconda3\envs\project\lib\site-
packages\seleniumwire\webdriver\browser.py", line 14
def __init__(self, *args, seleniumwire_options=None, **kwargs):
^
SyntaxError: invalid syntax
I am using python 2.7.16; selenium-wire 1.0.4.
from seleniumwire import webdriver
from selenium.common.exceptions import ElementClickInterceptedException,NoSuchElementException
import logging
def scrape_website(url):
# Configure browser driver
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--headless')
#Instantiate driver and navigate to URL
driver = webdriver.Chrome(chrome_options=options)
driver.implicitly_wait(30)
driver.get(url)
Am I missing something here?
Can't see anything wrong with your code :)
But selenium-wire 1.0.4 requires Python 3.4+.
You can find all the info on pypi.org - here
I am getting error issues all of a sudden with selenium and the chromedriver. I haven't changed a single thing yet I am met with these error messages. The script literally worked hours ago and now without any tweaks its not working.
traceback (most recent call last):
File "email.py", line 3, in <module>
from selenium import webdriver
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\__init__.py", line 18, in <module>
from .firefox.webdriver import WebDriver as Firefox # noqa
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 20, in <module>
import http.client as http_client
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 71, in <module>
import email.parser
File "C:\Users\Doe Labs\Desktop\Austin\Scripts\email.py", line 12, in <module>
options = webdriver.ChromeOptions()
Here is my corresponding code:
import pyautogui
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait
caps = DesiredCapabilities().CHROME
#caps["pageLoadStrategy"] = "eager"
options = webdriver.ChromeOptions()
options.add_argument(r'load-extension=C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper')
#options.add_argument('start-fullscreen')
options.add_argument('disable-infobars')
driver=webdriver.Chrome(desired_capabilities = caps, executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver', chrome_options=options)
driver.get('http://www.doelabs.com/')
driver.maximize_window()
Even more strange is that when open new terminal, load python, and type from selenium import webdriver, i dont get any errors. But, when I navigate to the folder where the script lives, and load python and type from selenium import webdriver, i get the error message that shows up above. I hope this can give some insight into my current predicament.
A few words about the solution :
email is a reserved word / keyword in Python Language, avoid using the word email within user defined filename/methods/classes.
pageLoadStrategy as eager is yet to be implemented in ChromeDriver, use either none or normal instead as per your requirement.
To maximize the Chrome Browser Window instead of maximize_window() use the argument start-maximized through ChromeOptions()
To load an extension use ChromeOptions as follows :
options.addExtensions(new File("/path/to/extension.crx"));
Here are the four methods to initialize Chrome Browser through ChromeDriver :
Vanila Method :
from selenium import webdriver
driver = webdriver.Firefox(r'C:\path\to\chromedriver.exe')
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()
Arguments as ChromeOptions :
from selenium import webdriver
options = webdriver.ChromeOptions()
options.addExtensions(new File("C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper.crx"));
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()
Capabilities as DesiredCapabilities :
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().CHROME.copy()
caps["pageLoadStrategy"] = "normal"
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', desired_capabilities=caps)
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()
Arguments as ChromeOptions and Capabilities as DesiredCapabilities :
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().CHROME.copy()
caps["pageLoadStrategy"] = "normal"
options = webdriver.ChromeOptions()
options.addExtensions(new File("C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\facebookpixelhelper.crx"));
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe', desired_capabilities=caps)
driver.get('http://www.doelabs.com/')
print("Page Title is : %s" %driver.title)
driver.quit()
You might want to change
executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver',
to
executable_path=r'C:\Users\Doe Labs\Desktop\Austin\sales_prospecting\chromedriver.exe',
You seem to have missed .exe, the extension of the executable file.