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

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

Related

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.

Selenium 4 add Binary path

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')

I got the error session not created: No matching capabilities found by Selenium Edge WebDriver

I got the error session not created: No matching capabilities found by Selenium Edge WebDriver.
The weird thing Edge version is 89.0.774.50, also the same as Webdriver.
What causes it?
from selenium import webdriver
from selenium.webdriver.support.select import Select
import time
driver = webdriver.Edge('/Users/mymac/Downloads/edgedriver_mac64/msedgedriver')
driver.get('mysite.com/signup')
time.sleep(1)
From the above-posted code, it looks like you have not added the options.use_chromium = True in your code. That might be causing this issue.
I suggest you refer to the example below and try to test it on your side.
from msedge.selenium_tools import Edge, EdgeOptions
options = EdgeOptions()
options.use_chromium = True
options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
driver = Edge(executable_path = r"Edge_driver_path_here....\msedgedriver.exe", options = options) # Modify the path here...
driver.get("https://Website_URL_here...")
This sample code working fine with the MS Edge 89.* version.
I have tested this code on the Windows machine, if you are using a MAC machine then you can try to make the necessary changes in code to run it there.
I added the following code and it worked! In MAC OS, you need to write like this.
desired_cap={}
driver = webdriver.Edge(('/Users/mymac/Downloads/edgedriver_mac64/msedgedriver', capabilities=desired_cap)

Using python selenium for Microsoft edge

I am trying to use pythons selenium for Microsoft edge but I keep getting this error:
WebDriverException: Message: unknown error: cannot find Microsoft Edge binary
I downloaded the latest version of the edge driver. Here is my code:
from selenium import webdriver
from selenium.webdriver.remote import webelement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
import time
from bs4 import BeautifulSoup
import os
from datetime import datetime
from selenium.webdriver import ActionChains
driver = webdriver.Edge(executable_path = 'C:\\Users\\Downloads\\edgedriver_win32\\msedgedriver.exe')
def get_trulia_estimate(address):
driver.get('https://www.trulia.com/')
print(address)
element = (By.ID, 'homepageSearchBoxTextInput')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(element)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(element)).send_keys(address)
search_button = (By.CSS_SELECTOR, "button[data-auto-test-id='searchButton']")
WebDriverWait(driver, 50).until(EC.element_to_be_clickable(search_button)).click()
time.sleep(3)
This post is quite old now, but hopefully I can help anyone that stumbles upon the same issue in future!
The problem is that you're using the wrong webdriver. Edge exists in two different versions, implemented on two non-interchangeable engines -- Chromium Edge and EdgeHTML (the default version at the time of writing). Each of these two versions has a different webdriver associated with it, with Chromium Edge's being "msedgedriver.exe", and EdgeHTML's being "MicrosoftWebDriver.exe".
You are using the EdgeHTML version of Edge, while trying to run the Chromium Edge webdriver. The 'cannot find Microsoft Edge binary' error Selenium spits out comes from this.
Luckily it is easy to install the right webdriver. If you have a Edge 17 or older, you can install the driver here. Make sure you download the EdgeHTML driver, not the Chromium driver, and add it to your PATH. For Edge 18 and later, you don't have to download anything. Simply run in the command prompt the command: DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0.
WebDriver cannot find your MS Edge path, u can try to uninstall and reinstall Edge.
If its not gonna help add Edge location to your system path or use --binary argument.
The Answer by James L is perfectly summarized. I have Microsoft EdgeHTML 18.17763 and I tried to, therefore, run the command:
DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0
This executed successfully. However, when running my code this time around, I get the error:
Message=A exception with a null response was thrown sending an HTTP
request to the remote WebDriver server for URL
http://localhost:52109/session. The status of the exception was
ReceiveFailure, and the message was: The underlying connection was
closed: An unexpected error occurred on a receive.
Looks like we need to, additionally, also enable developer options in Windows>>Settings>>Developer Options, which, since I do not have admin privileges, I am currently unable to do.
You must install msedge driver as You do with Chromedriver.
You first check wich version your edge is and then download de Edge driver from Microsoft Page
Download and unzip the msedgedriver file according your browser version
Install the package of it to python in terminal (pip install msedge-selenium-tools selenium==3.141)
Finally try the code to get the browser:
from selenium import webdriver
edgeBrowser = webdriver.Edge(r"C:....\msedgedriver.exe")
edgeBrowser.get('https://www.google.com')

Categories