Selenium is not accessing drivers properly - python

I am trying to access a browser using Selenium.
My first Python code try is this:
from selenium import webdriver
browser = webdriver.Chrome()
It gives me the error message:
'chromedriver' executable needs to be in PATH.
Some other answers on here suggest I point to the path manually. So I try:
from selenium import webdriver
chromedriver_loc = '/usr/local/bin'
driver = webdriver.Chrome(executable_path=chromedriver_loc)
This gives me the error message:
'bin' executable may have wrong permissions.
I am using a Mac and running an Anaconda Spyder environment.
The chromedriver file is in the /usr/local/bin. When I use GetInfo from the Finder program, the 'locked' selection is unchecked, but grayed out so I can't check or uncheck it.
The same error messages appear if I substitute Firefox for Chrome.
Can anybody help me provide the right 'permissions' so I can properly use Selenium? Please let me know if you need additional information, as this is my first question on here.

You should use the full path including the filename:
chromedriver_loc = '/usr/local/bin/chromedriver'

Related

How to solve Selenium WebDriver Error using Python?

I am learning Python to tried to do an extraction of data from another website. However, I wrote a simple code to try to open a Chrome browser window and display google on my web browser.
I have seen in other videos that it is only needed to write the following code to get this to work:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.google.com")
However, when I try to run it on my PyCharm, I got plenty of error lines. The same happen if I try to run it through my command prompt (both stating the same error - pictures below). I do not know what I am missing, but I should mention that I have downloaded the packages pip, selenium, selenium-chromedriver and I have also downloaded ChromeDriver separetely from the website https://chromedriver.chromium.org/
Could anyone please guide me through this issue? Thanks a lot everyone for your precious help and your time!
Kind regards,
Salvador
ChromeDriver_executable_location
Command_prompt
Python_codeError1
Python_codeError2
Packages_installed
Please post the errors so we can see what it is tell you is wrong.
I am also new to selenium and learning python.
First, make sure your Chromedriver is located in the same folder as your script.
Second, make sure your version of Chrome and Chromedriver are compatible.
Also, when the script is finish it will close the browser immediately.
I ran your posted code and it works for me. Did you have any errors when installing the packages?
I'm not sure why you get an error, but I know a workaround that might work for you.
Instead of using the webdriver file, you can use a service to install it for you, like this:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
You need to install the webdriver_manager library first using pip
Hope this helps
You could place the Chromedriver in system path and try to execute.
Go to Advanced System Settings.
Click on Environment variables.
Select Path variable and click on Edit.
Add the Chromedriver folder path.
Suppose the path for chromedriver.exe file is - C:/project/Chromedriver/chromedriver.exe, place C:/project/Chromedriver in the Path variable.
Restart the computer and then try to execute below lines:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.google.com")

Expected binary browser location - traceback in Python

I've just started building my first bot and I'm struggling with the first step: automating a browser.
Here's my code:
from selenium import webdriver
browser = webdriver.Firefox(executable_path="/Users/ker/Downloads/geckodriver")
browser.get("https://app.finxter.com/")
When I try running the code I get the following error:
selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
I've installed the geckodriver unix executable and specified the path, but for some reason it still won't work and I can't really understand the error message.
You may get this error message for two reasons:
Firefox isn't installed in your system.
Firefox isn't installed in the default location within your system.
Solution:
If Firefox isn't installed, install it on your system.
If firefox isn't installed at the default location, you need to pass the path of the firefox executable through an Option() instance:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = r"C:/location/firefox.exe"
driver = webdriver.Firefox(options=options, executable_path="/Users/ker/Downloads/geckodriver.exe")
driver.get('https://app.finxter.com/')
Please check if your driver is executable
you can change the permission with the command below.
chmod +x /Users/ker/Downloads/geckodriver

python-selenium.common.exceptions.WebDriverException: Message: Unknown error

I'm up with python 3.8 and selenium but recently I downloaded the latest edge web driver zip file and runned the mswdedriver.exe from that and typed this code in my ide:
from selenium import webdriver
browser = webdriver.Edge('‪F:\za\python\Assistant\msedgedriver.exe')
browser.maximize_window()
browser.get(url='http://seleniumhq.org/')
but I see this error:
selenium.common.exceptions.WebDriverException: Message: 'MicrosoftEdgeDriver' executable needs to be
in PATH. Please download from http://go.microsoft.com/fwlink/?LinkId=619687
Can you help me friends?
Thanks in advance.
You need to give a path to the webdriver executable when you load a webdriver, or have it stored as an environment variable:
webdriver.Edge(executable_path="path/to/executable")
A web driver is essentially a special browser application, you must install that application before you can run anything with it.
Here's Edge's web driver download page. Or you can use the link from the error message http://go.microsoft.com/fwlink/?LinkId=619687
Here's a similar question Python Selenium Chrome Webdriver
The backslashes in the executable path need to be escaped, per Python syntax:
browser = webdriver.Edge('‪F:\\za\\python\\Assistant\\msedgedriver.exe')
This problem appears to me
you must put in "Bin Folder" the file "MicrosoftWebDriver.exe" as is , edit the previous name of edge webdriver to be "MicrosoftWebDriver.exe" and put it in the "Bin Folder"
You need to download the browser driver from here
After the download completes, extract the driver executable to your preferred location. Add the folder where the executable is located to your PATH environment variable.

Selenium webdriver is opening the browser, but not opening the given url

This is the screenshot of the error I'm getting.
I have recently taken up the task to learn functional testing and web automation using selnium webdriver with python. When I execute my code, the web browser opens up, but the URL doesn't. I have tried all the suggestions on the internet such as: updating chrome, trying a different IDE, using FireFox. None of these have helped my code carry out what I want.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chrome = webdriver.Chrome(executable_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome')
chrome.get('https://www.youtube.com/watch?v=oM-yAjUGO-E')
For anyone encountering this issue, please make sure that you have installed the chromedriver (or whatever driver is available for your browser). For chrome you first need to check your information and find out the version from chrome://version/
then from, https://sites.google.com/a/chromium.org/chromedriver/downloads
download the corresponding driver and copy the filepath into the the executable_path section.
Ensure you have installed chromedriver.
The install path would typically be "/usr/local/bin", although whatever the path, that's the one to use as "executable_path"
Also remember to keep Chrome browser and chromedriver versions in sync i.e. if you have Chrome version 81, you should install chromedriver version 81.
I also have encountered this trouble. Before that, you're supposed to check the status code of your access. If it is 400, this station may set anti reptile which preventing any operation of reptile. I solved that by adding following code:
chrome.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})

Spyder randomly fails to locate chromedriver

I am using python via spyder to do some web scraping. My code seems to be working fine, but after a few times I open and close chromedriver during the same session of spyder, when I run
from selenium import webdriver
browser = webdriver.Chrome(executable_path = 'C:/Python34/Scripts/chromedriver.exe')
I get the error message
WebDriverException: 'chromedriver.exe' executable needs to be available in the path.
I tried downloading the latest version of chromedriver, but the issue persists. The suggestion I found here says to download the 64bit version of chromedriver, but I was not able to get a hold of that. Is this solvable anyhow? Any help much appreciated.
UPDATE:
Using chromedriver, this was solved using quit() instead of close() to end each session of chrome.
Try:
browser = webdriver.Chrome("C:/Python34/Scripts/chromedriver.exe")

Categories