Selenium 4 add Binary path - python

Good morning, I use selenium to do some webscraping, until yesterday everything worked fine, now I get this error, I know it is due to updating the binary, but as I want to share the programm, I would like the binary to be in the folder I created, so that it works with whoever opens the programme.
This is the code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from selenium.webdriver.support.ui import WebDriverWait
# options
options = Options()
options.use_chromium = True
options.add_argument("--headless")
options.add_argument("disable-gpu")
options.add_argument('--allow-running-insecure-content')
options.add_argument('--ignore-certificate-errors')
options.add_experimental_option('excludeSwitches', ['enable-logging'])
# Selenium driver path
s=Service("./Monatseinteilung/driver/msedgedriver.exe")
driver = webdriver.Edge(service=s, options=options)
this is the error:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of MSEdgeDriver only supports MSEdge version 100
Current browser version is 102.0.1245.30 with binary path C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe

You have to change your msedgedriver.exe driver to match the MS Edge version you have installed on your computer (102). You can download that from here. Replace your msedgedriver.exe and this script should start working. You will have to do that every time MS Edge updates.
Since you are using Python and since you want to share the program, this may not be very convenient. So instead, you can try libraries such as selenium_driver_updater, or webdriver_auto_update although webdriver_auto_update only seems to support Chrome. After that, you can check for the latest driver available every time you run your script
For selenium_driver_updater
filename = DriverUpdater.install(path=base_dir, driver_name=DriverUpdater.chromedriver, upgrade=True, check_driver_is_up_to_date=True, old_return=False)
driver = webdriver.Chrome(filename)
For webdriver_auto_update:
check_driver('folder/path/of/your/chromedriver')

Related

How to use a existing chrome installation with selenium webdriver?

I would like to use an existing installation of chrome (or firefox or brave browser) with selenium. Like that I could set prespecified settings / extensions (e.g. start nord-vpn when opening a new instance) that are active when the browser is opened with selenium.
I know there is selenium.webdriver.service with the "executeable-path" option, but it doesn't seem to work when you specify a specific chrome.exe, the usage seems to be for the chrome-driver only and then it still opens a "fresh" installation of chrome.
Starting selenium with extension-file I think is also not an option to use with the nord-vpn extension, as I have two-factor authentication active and login every single time would take too much time and effort, if possible at all.
Firefox profile
To use the existing installation of firefox you have to pass the profile path through set_preference() method using an instance of Option from selenium.webdriver.common.options as follows:
from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
profile_path = r'C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\s8543x41.default-release'
options=Options()
options.set_preference('profile', profile_path)
service = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")
You can find a relevant detailed discussion in Error update preferences in Firefox profile: 'Options' object has no attribute 'update_preferences'
Chrome profile
Where as to use an existing installation of google-chrome you have to pass the user profile path through add_argument() using the user-data-dir key through an instance of Option from selenium.webdriver.common.options as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
options = Options()
options.add_argument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://www.google.com/")
You can find a relevant detailed discussion in How to open a Chrome Profile through Python

How do I avoid a file path error while importing ChromeDriver?

I have been trying to work on my first web-scraping project for which I am using Selenium. However, I seem to be running into some issues with importing the ChromeDriver. I am using Selenium 3.0.0 and am working on Chrome.
webdriver_service = Service(ChromeDriverManager().install())
chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")
# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')
I keep getting the following message: 'chromedriver.exe' executable needs to be in PATH.
Let me know if there's some issue with the file path I am using as I think that is where the issue is coming from.
As answer tells, you should specify the chromedriver-path as following:
driver = webdriver.Chrome('/Users/MyUsername/Downloads/chromedriver.exe')
The executable_path= argument stands for chrome.exe, meaning google-chrome browser.
This should be done through the OS when possible and not through code as it needs to be done for every project this way but it is possible to set environment variables through code
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# Setting said environment variable
os.environ["CHROME_DRIVER_PATH"] = "Z:\\port\\driver\\chromedriver"
# Reading variable we created
DRIVER_PATH = os.environ.get('CHROME_DRIVER_PATH')
s=Service(DRIVER_PATH)
Got my answer from here
I'm going to be honest and say the path looks good to me. I even tried to run it in Visual Studio and it worked perfectly, so I have no idea why it isn't working for you. That being said I do have a way you can fix it. You can just not use executable_path. You already imported webdriver whose sole purpose is to handle the webdriver for selenium for you.
See here.
You don't even have to change much.
#pip install webdriver-manager, pip install selenium==3.0.0
import os
import logging
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")
# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(ChromeDriverManager().install())
#driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')
driver.get('https://www.google.com/')
Hope this helps.
You have to take care of a couple of things:
As you are using Selenium 3.0.0 you don't have to create any Service() object. So you can remove the line:
webdriver_service = Service(ChromeDriverManager().install())
Incase you don't intend to use ChromeDriverManager() you can remove the following lines:
# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
Unless you are executing your test in headless mode you won't require the argument --headless. So you can remove the line:
chrome_options.add_argument("--headless") # Ensure GUI is off
Unless you are executing your test as a root/administrator you won't nee the argument --no-sandbox. So you can remove the line:
chrome_options.add_argument("--no-sandbox")
Now you can download the matching ChromeDriver version from ChromeDriver page, unzip/untar it and use the following lines of code:
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/path/to/chromedriver') # for windows OS use chromedriver.exe
driver.get('https://www.google.com/')

Selenium firefox profile not working anymore

So I was happily using selenium together with Firefox and it seems that my firefox profile wouldn't load anymore some morning. It is driving me nuts to be honest. Whatever I try, the profile that is being used keeps being in the temp folder.
The following snippet is what I am doing
def getFireFoxBrowserWithUserFolder(folder) :
options = webdriver.FirefoxOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("user-data-dir=" + folder)
firefox = webdriver.FirefoxProfile(folder)
return webdriver.Firefox(options=options, executable_path=r'/home/bunsen/seleniumDriver/geckodriver', firefox_profile=firefox)
This worked up until very recent.
I got the newest version of the geckodriver(0.31.0), running it all on Debian stable, so my FF version is 91.10.0ESR (that is also what I see in the selenium browser).
I am at a loss at this point, anyone with the same problem?
first install webdriver_manager:
pip install webdriver-manager
then:
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver import Firefox
def getFireFoxBrowserWithUserFolder(folder) :
options = webdriver.FirefoxOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("user-data-dir=" + folder)
# here you can choose driver version
driver = Firefox(options = options, executable_path=GeckoDriverManager(version="2.26").install())
return driver
More informations about webdriver manager here.

'unresolved reference Error' 'from webdriver_manager.chrome import ChromeDriverManager

I am creating an application that shall interact with websites, the important code is listed below. So when it comes to importing 'ChromeDriverManager' from 'webdriver_manager.chrome' I get the Error "unresolved reference 'webdriver_manager'" and "unresolved reference'ChromeDriverManager'". The problem is that I want to autoupdate the chromedriver and this was the solution I came up with but it won't work when running it in pycharm but when I run it from the console it would work...does anyone have a solution on this because I prefer to test out my code in pycharm than starting it from the console every time.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager # important for chromedriver-autoinstall
import time
p= ChromeDriverManager()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"])
# disables 'Browser is managed by test software' and surpresses error with USB device
driver = webdriver.Chrome(executable_path=p.install(), options=chrome_options)
# see above + install right version of chromedriver
t = 2 # time to wait for input of number
i = 1 # current round
duration = 1
Had the same issue, as the package didn`t show up in the interpreter. I ended up just installing the webdriver-manager manually via settings.
Package installation interface

Python chrome opens with “Data;” with selenium

I am using python selenium for web scraping, but after running the below codes, chrome is launched but did not get the website as I want, instead, it shows 'data;' in url bar.
Could anyone help with the problem? Many thanks!!
PS: My chrome is 88.and chromedriver is also 88. the path of chrome and chromedriver are different, one is in desktop and the other is C://
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import random
option = Options()
option.add_experimental_option('excludeSwitches', ['enable-logging'])
option.add_argument('--remote-debugging-port=9222')
driver =webdriver.Chrome(executable_path='C:/Users/Desktop/chromedriver.exe')
driver.get("https://www.youtube.com")
Instead of using this
from selenium.webdriver.chrome.options import Options
options = options()
use this
options = webdriver.ChromeOptions()
and I would suggest you to put everything in the same project directory. which is a best practice.
Once you've put the chromedriver in the same directory.
do this:
from os import getcwd
driver = webdriver.Chrome(getcwd() + "\chromedriver.exe", options=options)
Also, try to close the old chrome windows.
It will go to data:// and after that it will redirect to your website.
And also follow Jiya's answer as well.

Categories