Selenium showing an error on installation and using webdriver - python

I am new to python currently learning how to automate browser. When I try to use the chrome webdriver with selenium then it also shows me an error which says
Traceback (most recent call last):
File "C:\Users\Ashu\Desktop\Python\chromedriver_win32\t.py", line 3, in <module>
browser = webdriver.Chrome("C:\\Users\\Ashu\\Desktop\\Python\\chromedriver_win32\\chromedriver.exe")
File "C:\Users\Ashu\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
self.service.start()
File "C:\Users\Ashu\AppData\Local\Programs\Python\Python36-32\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:\Users\Ashu\Desktop\Python\chromedriver_win32\chromedriver.exe
I also tried to do it with other browsers using their respective webdrivers but all of them are throwing the same error.
Anyone please help me to get this problem fixed.
Any help is appreciated.

Seems like you have a version of the requests package which is incompatible with the version of Selenium you're trying to install. From the first output, it says your version is newer than what is required, so try downgrading your requests package to version 2.0.0. You can do so with the following command:
pip install requests==2.0.0
Hope this helps!

Related

Chromium-Chromedriver missing library libgfx on Raspberry Pi

I'm trying to run Chromium in headless mode on Raspberry Pi 3. I got Chromium-chromedriver from this repository. I keep getting the same error. When I try running chromium-chromedriver from terminal it says I am missing libgfx.so, which after intensely looking for I can't find anywhere for Raspberry Pi. I aslo tried installing from the terminal. Below is my code along with everything I have tried and the correspodning errors. Any and all help is appreciated at this point.
My code:
from selenium.webdriver import Chrome
from contextlib import closing
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
with closing(Chrome(chrome_options=chrome_options)) as driver:
driver.get(URL)
Gives the error:
python3 Chromium\ Launch\ Test.py
Traceback (most recent call last):
File "Chromium Launch Test.py", line 3, in <module>
with closing(Chrome(chrome_options=chrome_options)) as driver:
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
self.service.start()
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 98, in start
self.assert_process_still_running()
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 111, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service /usr/lib/chromium-browser/chromedriver unexpectedly exited. Status code was: 12
Trying to run chromium-chromedriver from terminal:
/usr/lib/chromium-browser/chromedriver: error while loading shared libraries: libgfx.so: cannot open shared object file: No such file or directory
Trying to install libgfx from terminal:
sudo apt install libgfx
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libgfx
Literally no clue where to go. Tried updating before launching. Tried updating all dependent libraries listed in the repository individually. Nothing works.
I've been fighting this exact same issue. After setting up an ubuntu vm and completing my dev there so I knew solution worked I wanted to move it back to my pi. I found this driver and it works https://github.com/electron/electron/releases/download/v1.6.0/chromedriver-v2.21-linux-armv7l.zip

Calling the Firefox webdriver in python

I'm using Debian 9 Stretch and Pycharm IDE and trying to learn web-scramping; I installed the Selenium package simply by running:
pip install selenium
and the Firefox webdriver by running:
wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz
tar -xvzf geckodriver-v0.19.1-linux64.tar.gz.1
chmod +x geckodriver
respectively, to download the last release, extract it and make the driver executable. After that, I added the driver to the following path:
usr/local/bin
I ran all by using the Pycharm IDE terminal and not the built-in Debian terminal.
In order to open Firefox and web-scrape, I run:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
webdriver.Firefox(executable_path="/usr/local/bin/geckodriver")
The last line gives an error message as output:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/root/PycharmProjects/Example/venv/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 162, in __init__
keep_alive=True)
File "/root/PycharmProjects/Example/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/root/PycharmProjects/Example/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/root/PycharmProjects/Example/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/root/PycharmProjects/Example/venv/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
I'm a newbie both in Python and in web-scraping; please, could someone explain what does go wrong with installation and coding and why I got this error?
In the hope to be clear asking the question, I thanks all in advance for the help.
Assuming that the location of the geckodriver is correct, you can check below:
properties of the geckodriver should have the correct permission for the user. You would need to check the box "Allow this file to run as a program" or
if you you have restricted access, save the geckodriver in your home/username/geckodriver then path it to your firefox. Saving it in your home folder will be able to modify the properties of your geckodriver.
[EDIT] Are your running in command line? If so, you need a virtual display, I have used pyvirtualdisplay:
from pyvirtualdisplay import Display
display = Display(visible=0,size(800, 600))
display.start()
driver = webdriver.Firefox(executable_path="/usr/local/bin/geckodriver")
The problem has been solved by removing Mozilla Firefox, that, in Debian 9 Stretch is installed as ESR (Extended Release Support) by default; at the time the Firefox ESR version was 52.0.
After, I installed the by instaling the unstable Firefox version (not Beta) by running on the the terminal as super-user:
su -
gedit /etc/apt/sources.list
and adding deb http://ftp.it.debian.org/debian/ unstable main to the sources list file.
After, I ran:
apt-get update
apt-get install -t unstable firefox
to update the software and install Firefox.
By following the guidelines explained in the question to install and run the selenium Python package everything should work fine (at least, for me!).
Hope this will help other users too!
While woking with Selenium-Python Client v3.10.0 along with GeckoDriver v0.19.1 and Firefox v58.0.2 you have to initialize the WebDriver instance and assign it to a variable, which will in-turn initialize the Web Browser which will in-turn open the desired URL as follows :
from selenium import webdriver
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()

Error when using chrome driver when attempting to execute python script

Having issue trying to automate when using python script. I have access to repositories by GIT, where the python folders all use Python 2.7 syntax. I also have chrome driver as well(Using ubuntu). When I attempt to automate, I just get error, regarding python 2.7. Unsure if thats the issue.
I was given a python code, that would use chrome driver to test if I can automate with the python code that is scripted for use once executed. Using the command terminal, I get the following error:
traceback (most recent call last):
File "autotimesheet.py", line 254, in <module>
processPDCI(res)
File "autotimesheet.py", line 85, in processPDCI
driver = webdriver.Chrome('/home/myname/ChromeDriver')
File "/home/myname/.local/lib/python2.7/site-
packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
self.service.start()
File "/home/myName/.local/lib/python2.7/site-
packages/selenium/webdriver/common/service.py", line 86, 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
So unsure how to solve it. Friend that is trying to help me is also stumped. I have use pip install selenium & pip install nose for set up. Unsure if I need to install anything else. So any guidance/help would be appreciated. Anything that is vague, I will try to provide better detail. Hopefully this question is related to this section of stackoverflow. Thanks for reading
just an update. I was able to automate the script. Here was the only step I did: In the autotimesheet.py, I change the line of code of this:
driver = webdriver.Chrome('/home/myname/ChromeDriver/)
to:
driver = webdriver.Chrome('/home/myname/ChromeDriver/chromedriver')
where the last part is the executable file. So it opened up chrome and started to automate. Now that I know this can automate, have to fix the issue with the repositories part. With my .profile, I have this:
export SELENIUM_CHROME_DRIVER=/home/myname/ChromeDriver/chromedriver.exe
So either I have to fix this, or its another path/directory or r/w issue. Thanks for the comments. I will resolve in a few hours once I can get this fix!
-----------UPDATE-----------
Being able to fix the automation python script issue, I have to tackle the last issue. I will still follow the comments, and will troubleshoot. My error for future reference if anyone has similar problem:
ERROR: tests.test_to_run.TestsToRun.test_run_this_test
Traceback (most recent call last):
File "/home/myName/.local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File
"/home/myname/Automation/pdci_keyword/python/tests/test_to_run.py",
line 25, in test_run_this_test
self.test_controller.insert_steps_from_script_into_list_array()
File
"/home/myName/Automation/pdci_keyword/python/lib/
web_interface/test_controller.py", line 337, in
insert_steps_from_script_into_list_array
self.process_msg_insert(data)
File
"/home/myName/Automation/pdci_keyword/python/lib/web_interface/
test_controller.py", line 200, in process_msg_insert

Using Selenium, Python script to open Firefox, not going to URL provided

Problem
I'm following of the examples listed on the documentation here. Basically, just trying to get Selenium to open Firefox, take me to a site, make a query and then close the browser.
I've been running into an issue where Selenium will open the browser, but then not go to the specified website. In other cases, I receive an error, I should mention this error only appear after upgrading Selenium.
Traceback (most recent call last):
File "script.py", line 4, in <module>
browser = webdriver.Firefox()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 142, in __init__
self.service.start()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
I'm using the latest version of Firefox, 54.0.1
I'm using Python version 2.7.11
And I have Selenium version 3.4.3
script.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('http://www.yahoo.com')
assert 'Yahoo!' in browser.title
elem = browser.find_element_by_name('p') # Find the search box
elem.send_keys('seleniumhq' + Keys.RETURN)
browser.quit()
In this case, these were the steps I took to solve this issue.
Updated Firefox to the latest version
Updated Selenium as well
Removed incorrect version of geckodriver from bin folder
Updated xcode in App Store
Used brew install geckodriver to get correct version into bin folder
Used brew link
Ran script.py

geckodriver executable needs to be in path

I have read previous questions asked on this topic and tried to follow the suggestions but I continue to get errors. On terminal, I ran
export PATH=$PATH:/Users/Conger/Documents/geckodriver-0.8.0-OSX
I also tried
export PATH=$PATH:/Users/Conger/Documents/geckodriver
When I run the following Python code
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/Users/Conger/Documents/Firefox.app'
driver = webdriver.Firefox(capabilities=firefox_capabilities)
I still get the following error
Python - testwebscrap.py:8
Traceback (most recent call last):
File "/Users/Conger/Documents/Python/Crash_Course/testwebscrap.py", line 11, in <module>
driver = webdriver.Firefox(capabilities=firefox_capabilities)
File "/Users/Conger/miniconda2/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 135, in __init__
self.service.start()
File "/Users/Conger/miniconda2/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 71, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x1006df6d0>> ignored
[Finished in 0.194s]
you may downgrade your selenium by
pip install selenium==2.53.6
This has solved my issue
On mac:
brew install geckodriver
Homebrew is the most popular package manager for Mac OS X, you will need install XCode on your mac and it will be then accesible from your terminal.
You can follow this tutorial if required
First we know that gekodriver is the driver engine of Firefox,and we know that
driver.Firefox() is used to open Firefox browser, and it will call the gekodriver engine ,so we need to give the gekodirver a executable permission.
so we download the latest gekodriver uncompress the tar packge ,and put gekodriver at the /usr/bin/
ok,that's my answer and i have tested.
I just downloaded the latest version geckodriver (I have win7) from here and added that exe-file in python directory (which already in PATH)
export path works only in the terminal you have entered the command. If you try to run the script from a different terminal, you will get the same error.

Categories