I know there are already a couple threads about this, but I've went over them all, and this is my 3rd day trying to break through and haven't made any improvements. Here is the situation:
I'm trying to use this https://github.com/tejavoo/GooglePlayReviewScraper/blob/master/scraper.py to create a google play review scraper for my research, but continue recieving the following error:
Conda3/Python38/Windows10/Spyder 4.0.1
I've made sure that the chromedriver is in the PATH, and I've made sure it as all permissions, along with spyder and python. I also made sure it was the correct version that matches my google chrome.
I've downloaded the chromedriver, unzipped it into the folder I'm working in.
I also didn't have the Permission error (Winerror 5) Until I recently updated to spyder 4.
Please, what am I missing?
chromedriver = "C:\\Users\\james\\Downloads\\ML\\New folder\\chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1200x600') # optional
driver = webdriver.Chrome(executable_path = chromedriver, chrome_options = options)
The Error
Traceback (most recent call last):
File "C:\Users\james\.conda\envs\spyder-4.0.0_1\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\james\.conda\envs\spyder-4.0.0_1\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 104, in __init__
super(SubprocessPopen, self).__init__(*args, **kwargs)
File "C:\Users\james\.conda\envs\spyder-4.0.0_1\lib\subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "C:\Users\james\.conda\envs\spyder-4.0.0_1\lib\subprocess.py", line 1207, in _execute_child
startupinfo)
*PermissionError: [WinError 5] Access is denied*
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\james\Downloads\ML\New folder\PlayReviews.py", line 37, in <module>
driver = webdriver.Chrome(executable_path = chromedriver, chrome_options = options)
File "C:\Users\james\.conda\envs\spyder-4.0.0_1\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\james\.conda\envs\spyder-4.0.0_1\lib\site-packages\selenium\webdriver\common\service.py", line 88, in start
os.path.basename(self.path), self.start_error_message)
WebDriverException: 'chromedriver.exe' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
And if I change the code and place '' around chromedriver like this
chromedriver = ("C:\\Users\\james\\Downloads\\ML\\New folder\\chromedriver.exe")
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1200x600') # optional
driver = webdriver.Chrome(executable_path= 'chromedriver', chrome_options = 'options')
I get this
runcell(0, 'C:/Users/james/Downloads/ML/New folder/PlayReviews.py')
C:\Users\james\Downloads\ML\New folder\PlayReviews.py:37: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(executable_path= 'chromedriver', chrome_options = 'options')
Traceback (most recent call last):
File "C:\Users\james\Downloads\ML\New folder\PlayReviews.py", line 37, in <module>
driver = webdriver.Chrome(executable_path= 'chromedriver', chrome_options = 'options')
File "C:\Users\james\.conda\envs\spyder-4.0.0_1\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 64, in __init__
desired_capabilities = options.to_capabilities()
AttributeError: 'str' object has no attribute 'to_capabilities'
And a webdriver.py page opens
Any help is much appreciated!
Cheers :)
Update
I tried the raw string idea as mentioned
chromedriver = "C:/Users/james/Downloads/ML/New folder/PlayReviews.py"
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1200x600') # optional
driver = webdriver.Chrome(executable_path = r'C:\\Users\\james\\Downloads\\ML\\New folder\\chromedriver.exe', chrome_options='options')
And it seems to maybe pass it? As now I am on to this error :
runcell(0, 'C:/Users/james/Downloads/ML/New folder/PlayReviews.py')
C:\Users\james\Downloads\ML\New folder\PlayReviews.py:39: DeprecationWarning: use options instead of chrome_options
driver = webdriver.Chrome(executable_path = r'C:\\Users\\james\\Downloads\\ML\\New folder\\chromedriver.exe', chrome_options='options')
Traceback (most recent call last):
File "C:\Users\james\Downloads\ML\New folder\PlayReviews.py", line 39, in <module>
driver = webdriver.Chrome(executable_path = r'C:\\Users\\james\\Downloads\\ML\\New folder\\chromedriver.exe', chrome_options='options')
File "C:\Users\james\.conda\envs\spyder-4.0.0_1\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 64, in __init__
desired_capabilities = options.to_capabilities()
AttributeError: 'str' object has no attribute 'to_capabilities'
However, this error, actually comes from line 64 of the webdriver.py which opens as a result of running the former
if chrome_options:
warnings.warn('use options instead of chrome_options',
DeprecationWarning, stacklevel=2)
options = chrome_options
if options is None:
# desired_capabilities stays as passed in
if desired_capabilities is None:
desired_capabilities = self.create_options().to_capabilities()
else:
if desired_capabilities is None:
desired_capabilities = options.to_capabilities()
else:
desired_capabilities.update(options.to_capabilities())
I have seen similar permission errors when the executable path was not a raw string. Have you tried formatting it like this?
driver = webdriver.Chrome(executable_path = r'C:\\Users\\james\\Downloads\\ML\\New folder\\chromedriver.exe', chrome_options = options)
I've finally managed to figure out where I went wrong.
While I had the PATH and all correct - the Chromdriver.exe file was not in my Python/scripts folder.
After setting it there, re-placing the PATH, all works well!! :)
Here is part of a little script I made to login and check for new questions on here. It is in a different library in C# but maybe it is still relevant and will help you discover the issue. Anyways the code is below hope it helps.
The only thing you aren't doing that I am is setting the BinaryLocation on the ChromeOptions and I am discovering the driver directory at run-time.
Also, sometimes the error messages like those are caused by exactly what they say is wrong. Maybe in order to run the selenium driver it may require more elevated windows permissions like when you right click and say run as administrator on programs sometimes. Have you tried running the terminal/command line that you are trying to run the python script on as an admin?
ChromeOptions options = new ChromeOptions();
options.BinaryLocation = #"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
using (IWebDriver driver = new ChromeDriver(GetChromeDriverDirectory(), options))
{
driver.NavigateToUrl("https://stackoverflow.com/questions/tagged?sort=Newest&filters=NoAnswers&tagMode=Watched");
}
// Here is the method I use to get the Chrome driver directory.
private static string GetChromeDriverDirectory() => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
You need to take care of a few things:
While using Selenium's python client, to pass the absolute path of the ChromeDriver you need to use the single forward slash along with the raw i.e. r switch.
The argument chrome_options is now deprecated, instead you need to use options.
Your effective code block will be:
options = webdriver.ChromeOptions()
options.headless = True
# previously: options.set_headless(headless=True)
# long ago: options.add_argument('--headless')
options.add_argument('--window-size=1200x600')
driver = webdriver.Chrome(executable_path = r'C:\Users\james\Downloads\ML\New folder\chromedriver.exe', options = options)
References
You can find a couple of relevant discussions in:
'Webdrivers' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?
Related
I want my program to open up the default chrome profile and then get youtube.
I can make it either open youtube (in a new chrome browser), or open the default chrome profile, but not both.
(And no im not running both driver variables)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
print('starting')
print('getting driver')
exec_path= "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" # exec path from chrome://version
profilePath= 'C:\\Users\\MyName\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1' #profile path from chrome://version
chromePath= 'C:\\Users\\MyName\\OneDrive\\Documents\\Python programming\\chromedriver.exe' #path to driver
options= webdriver.ChromeOptions()
options.add_argument(profilePath)
print('options add argument...')
### Run one or the other ###
driver = webdriver.Chrome(executable_path= chromePath , options=options) #gets youtube
driver = webdriver.Chrome(executable_path= exec_path, options=options) #gets chrome profile
print('webdriver getting youtube...')
driver.get("https://www.youtube.com/")
when I run the driver line that gets the chrome profile I get the error:
Traceback (most recent call last):
File "C:\Users\MyName\OneDrive\Documents\Python programming\Web automation\webAuto.py", line 24, in
driver = webdriver.Chrome(executable_path= exec_path, options=options) #gets chrome profile
File "C:\Users\MyName\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in init
self.service.start()
File "C:\Users\MyName\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py", line 104, in start
raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
As per your question and your code trials if you want to open a Chrome with the customized Chrome Profile, Try using below code.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('C:\\Users\\MyName\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1' )
driver = webdriver.Chrome(executable_path='C:\\Users\\MyName\\OneDrive\\Documents\\Python programming\\chromedriver.exe', chrome_options=options)
driver.get("https://www.youtube.com/")
Here you will find a detailed discussion on How to open a Chrome Profile through Python
I have been following tutorials, and I came up on Firefox, and using Options(), I could set headless mode while defining browser (browser = Firefox(options=opts)), but for Firefox only. I downloaded the driver for the new chromium Edge, but I cannot seem to set the options= keyword in Edge(). Can anyone tell me how do I set the options while defining the browser?
from selenium.webdriver import Edge
from selenium.webdriver.edge.options import Options
opts = Options()
opts.headless = True
browser = Edge(options=opts)
# ^^^^^
It seems there is no options keyword, and I get an error:
Traceback (most recent call last):
File ".\tutorial.py", line 6, in <module>
browser = Edge(options=opts)
TypeError: __init__() got an unexpected keyword argument 'options'
Any help will be appreciated.
Thanks in advance!
For Edge Chromium you need to install msedge-selenium-tools package for python and then you can initialize the driver
from msedge.selenium_tools import EdgeOptions
options = EdgeOptions()
options.use_chromium = True
driver = Edge(options)
Example code:
from selenium import webdriver
browser = webdriver.Chrome()
browser.minimize_window()
Returns the following exception:
File "myScript.py", line 4, in <module>
browser.minimize_window()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 738, in minimize_window
self.execute(Command.MINIMIZE_WINDOW)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 208, in check_response
raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: unknown command: session/8252be05ea571a2c623450db8ba097c0/window/minimize
Adding the line
print dir(browser)
Shows that minimize_window() is a listed function of browser. So what gives? Is this functionality just not compatible with Chrome?
Python 2.7
This error message...
selenium.common.exceptions.WebDriverException: Message: unknown command: session/8252be05ea571a2c623450db8ba097c0/window/minimize
...implies that the call to minimize_window() function wasn't recoznized.
You spotted it correct. As now WebDriver Specification is a W3C Recommendation the function defination for maximizing the window was adjusted as per the W3C Recommended Specification as follows:
def maximize_window(self):
"""
Maximizes the current window that webdriver is using
"""
params = None
command = Command.W3C_MAXIMIZE_WINDOW
if not self.w3c:
command = Command.MAXIMIZE_WINDOW
params = {'windowHandle': 'current'}
self.execute(command, params)
But, the function defination for minimizing the window is still pending to be W3C compliant within the Python Client as is still defined as:
def minimize_window(self):
"""
Invokes the window manager-specific 'minimize' operation
"""
self.execute(Command.MINIMIZE_WINDOW)
Hence you see the error:
unknown command: session/8252be05ea571a2c623450db8ba097c0/window/minimize
Solution
As the default/common practice is to open the browser in maximized mode, while Test Execution is In Progress minimizing the browser would be against the best practices as Selenium may loose the focus over the Browsing Context and an exception may raise during the Test Execution. However, Selenium's python client does have a minimize_window() method which eventually pushes the Chrome Browsing Context effectively to the background.
Python:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
driver.minimize_window()
Java Solution:
driver.navigate().to("https://www.google.com/");
Point p = driver.manage().window().getPosition();
Dimension d = driver.manage().window().getSize();
driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
Just tried to downgrade my chromedriver version to 2.25 and got...
selenium.common.exceptions.WebDriverException: Message: unknown command: session/f35727d2129895c35b24deeb7090eb26/window/minimize
with the same code.
But if to use the last (2.43) it work just fine
So just upgrade to up-to-date chromedriver version to be able to use minimize_window method
I have read the api doc on how to setup the the chrome webdriver but I am a little new to **kwargs and I have to pass the executable_path as a dictionary entry, can some one tell me what am I doing wrong in my code?
executable_path = {'executable_path':'/usr/bing/firefox-esr'}
browser = Browser('firefox', **executable_path)
this is the error im getting
Traceback (most recent call last):
File "logger.py", line 10, in <module>
browser = Browser('firefox', **executable_path)
File "/usr/local/lib/python2.7/dist-packages/splinter/browser.py", line 63, in Browser
return driver(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'executable_path'
and this is the code that is supplied by the splinter website as an example
executable_path = {'executable_path':'</path/to/chrome>'}
browser = Browser('chrome', **executable_path)
You specified 'firefox' instead of 'chrome', and Splinter's firefox driver does not have the executable_path argument.
See the source for Splinter 0.7.4: https://github.com/cobrateam/splinter/blob/e23df9c7e0ba9d1b495292cf8c23c0fe4192b26d/splinter/driver/webdriver/firefox.py#L20
Nvm guys, the problem was I didn't have a geckodriver in usr/local/bin folder and then you can set browser = Browser ('firefox')
I wrote this code which is in selenium webdriver in python.
I want to open link in chrome browser, the browser is getting started but can't fetch url.
def setUp(self):
self.browser = webdriver.Chrome("/usr/bin/google-chrome")
browser = self.browser
browser.implicitly_wait(5)
browser.get("http://www.google.com")
This gives this error:
ERROR: test_start (__main__.Saletest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "sale_test.py", line 16, in setUp
self.browser = webdriver.Chrome("/usr/bin/google-chrome")
File "/usr/local/lib/python2.7/dist- packages/selenium/webdriver/chrome/webdriver.py", line 60, in __init__
self.service.start()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/service.py", line 88, in start
os.path.basename(self.path) + "'")
WebDriverException: Message: Can not connect to the 'google-chrome'
Basically Selenium find your binary, but can't connect to it.
Try this, this seems to correspond to your problem :)
WebDriverException: Message: 'Can not connect to the ChromeDriver'. Error in utils.is_connectable(self.port):
The first argument that webdriver.Chrome expects to be provided with is the executable_path - a path to the chromedriver, but you are passing a path to the Chrome itself.
The executable_path argument is optional. In case chromedriver is in PATH and selenium can automatically find it, you don't need to explicitly state where it is located.
If you want to provide a custom Chrome browser executable instead, define the Chrome binary location via ChromeOptions:
options = Options()
options.binary_location = "/path/to/google-chrome"
driver = webdriver.Chrome(chrome_options=options)
And, just for completeness, here is how would the setup look in case you have a custom chromedriver path and a custom Chrome browser binary path:
options = Options()
options.binary_location = "/path/to/google-chrome"
driver = webdriver.Chrome(executable_path="/path/to/chromedriver",
chrome_options=options)