Expected binary browser location - traceback in Python - 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

Related

Python selenium gives data; in Chrome browser

Trying to create a webscraper via Selenium. Below code only gives data; in the browser and ends up with error.
from selenium import webdriver
options = webdriver.ChromeOptions()
website = ""
path = "/Users/lilly/Downloads/chromedriver"
driver = webdriver.Chrome(path)
driver.get(website)
#all_matches_button = driver.find_element_by_xpath('')
#all_matches_button.click()
#driver.quit()
Chrome browser: Version 104.0.5112.81 (latest)
Chrome driver: 104.0.5112.79 (latest)
Error message in Python:
selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
Tried to reinstall Chrome driver, but not luck. (https://sites.google.com/chromium.org/driver/)
Currently working on my Windows, but above code works on my macbook. I noticed on my Macbook that the version of the Chrome browser and driver are the same (104.0.5112.79).
Also have to close all browser tabs before running the code. otherwise it will result in the below error:
InvalidArgumentException: Message: invalid argument: user data directory is already in use error using --user-data-dir
Also quite annoying..
Thanks.
L.
For Windows, you have to provide the file extension. In your Mac, the chrome driver executable file name is chromedriver. But for Windows, it is chromedriver.exe.
Change your path to the following and it will work. Make sure the executable file path is provided not just the folder.
path = "/Users/lilly/Downloads/chromedriver.exe"

why wont selenium module work properly for me

I installed the selenium module with pip installer. Then I tried to make the code to open firefox, then open a new tab to go to google.
Code:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.google.com")
The expected result should have opened firefox and then go to google.com.
But what actually happened was the program didn't produce any error, it just didn't open firefox, and the screen wasn't frozen either.
Download a matching version of Geckodriver and unpack the geckodriver.exe to the location where current user can execute programs from (normally it's any place inside your home folder)
Amend your code to include the location of the aforementioned geckodriver like:
driver = webdriver.Firefox(executable_path="/path/to/the/geckodriver/binary")
If this doesn't help - provide the path to the Firefox executable as well:
driver = webdriver.Firefox(executable_path="/path/to/the/geckodriver/binary", firefox_binary="/path/to/firefox/binary")
Instead of steps 2 and 3 you can add both firefox and geckodriver to your OS PATH
References:
Selenium with Python - Getting Started
Selenium With Python
Selenium using Python - Geckodriver executable needs to be in PATH

selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH with GeckoDriver Firefox Selenium and Python

I am trying to open Firefox with selenium,i tried
from selenium import webdriver
driver=webdriver.Firefox()
But i got the following error:
selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH.
Selenium using Python - Geckodriver executable needs to be in PATH
I tried
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('/usr/bin/firefox')
browser = webdriver.Firefox(firefox_binary=binary)
Also tried
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities.FIREFOX
caps['marionette'] = True
caps['binary'] = '/usr/bin/firefox'
d = webdriver.Firefox(capabilities=caps)
`but still did not work.
However, when i tried using the above code replacing the last line with
d=webdriver.Firefox(capabilities=caps,executable_path='/usr/bin/firefox') and having my Firefox closed from background it would open Firefox but I can't simply d.get("https://www.google.com") it gets stuck on Linux homepage and doesn't open anything.
After typing whereis firefox in terminal i got /usr/bin/firefox,also if it matters i use python 2.7
Note: I hope this isn't a duplicate of the above link because i tried the answers and it didn't fix it.
I installed geckodriver from github, and tried browser=webdriver.Firefox(executable_path="geckodriver") ,I have placed the driver is the same directory.
It is still not clear why you are seeing the error as:
selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH.
In majority of the cases the common PATH related error is associated with geckodriver.
However, while working with Selenium 3.x you need to download the latest GeckoDriver from mozilla/geckodriver and save it anywhere in your system and provide the absolute path of the GeckoDriver through the argument executable_path.
The following code block works perfecto to open Firefox Nightly Browser (installed at customized location):
Code Block:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.binary_location = '/path/to/firefox'
driver = webdriver.Firefox(firefox_options=options, executable_path='/path/to/geckodriver')
driver.get('http://google.com/')
print("Page title is: %s" %(driver.title))
driver.quit()
Console Output:
Page title is: Google

Selenium on MAC, Message: 'chromedriver' executable may have wrong permissions

I'm just trying to do something very basic on my Mac using selenium and I can't even open a webpage. I'm getting an error of :
Traceback (most recent call last):
File "/Users/godsinred/Desktop/InstagramLiker/GmailAccountGenerator.py", line 10, in <module>
driver = webdriver.Chrome()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 88, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
Here is my code below:
from selenium import webdriver
import time
link = "https://accounts.google.com"
driver = webdriver.Chrome()
driver.get(link)
time.sleep(5)
driver.quit()
Most answers here and in other related posts suggest users to just move the file to /usr/bin and they work fine if you are just running chromedriver locally and normally.
However, if you are compiling Python scripts into executables using compilers such as cx_freeze, you may not be able to afford the luxury if your program always uses a relative link to chromedriver.
As the error message suggests, your compiled program does not have the permissions to manipulate chromedriver. To use a relative link to chromedriver on a Mac in your compiled Python program, you can programmatically change the permission of chromedriver in your Python script using:
import os
os.chmod('/path/to/chromedriver', 0755) # e.g. os.chmod('/Users/user/Documents/my_project/chromedriver', 0755)
You can test this by doing the following:
cd to your working directory
$ chmod 755 chromedriver to allow your program to manipulate it
P.S. 755 is the default numerical permission for files in usr/bin. 664 is the default numerical permission for files in other normal folders (probably your working directory). Thus, when chromedriver complains it does not have the correct permission, you need to grant it a numerical permission equivalent to or greater than 755.
The error says it all :
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
The error clearly mentions that the chromedriver which is getting detected have wrong permissions.
Solution
Download the latest chromedriver binary from ChromeDriver - WebDriver for Chrome and save it in your system.
Ensure that chromedriver binary have the required permissions.
While initiating the WebDriver and WebClient pass the argument executable_path along with the absolute path of the chromedriver binary as follows :
from selenium import webdriver
link = "https://accounts.google.com"
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
driver.get(link)
Reference
You can find a detailed relevant discussion in:
'Webdrivers' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
If you are on windows give path including file name. For example,
'./chromedriver/chromedriver.exe'
My line of code looks like below.
service = webdriver.chrome.service.Service('./chromedriver/chromedriver.exe')
This worked! I followed these instructions to update PATH: https://www.kenst.com/2015/03/installing-chromedriver-on-mac-osx/
I dragged my chromedriver.exe from Finder into Terminal (/etc/paths), and then copied the address in Terminal and dropped it into my Python IDE where the PATH should be inserted.
Check out a this topics
1- If you are using Linux, access the folder containing the file
Chromedriver.exe set on 755
2- check correct path for Chromedriver.exe file in your code
3- If you are using Windows servers,check the Chromedriver.exe file is available for the current user (not only the admin does have access to Chromedriver.exe - see in c://users...)
What worked for me in Windows was adding the location of the driver to Windows local PATH var, restarting my python environment so that the path to the driver displayed after running this:
import os;path = os.getenv('PATH'); print(path);
and then I did not specify the path when loading the driver:
from selenium import webdriver
driver = webdriver.Chrome()
if I tried putting the path in the Chrome() call it caused the permissions error. Adding it to the local environment path is enough.

Selenium is not accessing drivers properly

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'

Categories