I have been working on a scrapy project and have used Selenium with Firefox webdriver. Everything works fine. But now I have to use PhantomJS with Selenium. And I am getting a weird error.
Here is the code snippet I am trying to run to test that PhantomJS works with Selenium.
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550)
driver.get("https://duckduckgo.com/")
driver.find_element_by_id('search_form_input_homepage').send_keys("realpython")
driver.find_element_by_id("search_button_homepage").click()
print(driver.current_url)
driver.quit()
I am getting this error when I run the above code.
Traceback (most recent call last):
File "phantom_try.py", line 2, in <module>
driver = webdriver.PhantomJS(
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/phantomjs/webdriver.py", line 56, in __init__
desired_capabilities=desired_capabilities)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 91, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 173, in start_session
'desiredCapabilities': desired_capabilities,
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 165, in check_response
raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message:
However, everything works fine on the above code if I replace the PhantomJS() initialization line with driver = webdriver.Firefox(). The output https://duckduckgo.com/?q=realpython is displayed. But no luck with PhantomJS.
# Here is the version of tools I am using.
Nodejs v0.10.25
PhantomJS 1.9.8
Scrapy 1.0.5
Selenium 2.53.2
I did apt-get install for Nodejs, npm and PhantomJS while pip install for scrapy and selenium. Before this, I also tried installing PhantomJS by downloading source, extracting and copying executable to bin. Also, installed PhantomJS using npm. All methods are showing the same error.
Related
OS : AWS EC2 Ubuntu 22.04.1 LTS
Python : 3.10.6
Firefox : Mozilla Firefox 107.0
Geckodriver : 0.32.0 (2022-11-10)
UFW(Ubuntu Firewall) : Inactive
Others : latest version
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.firefox.options import Options as FirefoxOption
from webdriver_manager.firefox import GeckoDriverManager
options = FirefoxOption()
options.headless = True
service = FirefoxService(GeckoDriverManager().install())
# <selenium.webdriver.firefox.service.Service object at 0x7f008f2a7c70>
driver = webdriver.Firefox(
service=service,
options=options
)
When I ran this code
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/ubuntu/.local/share/virtualenvs/tool-lKp4P5xD/lib/python3.10/site-packages/selenium/webdriver/firefox/webdriver.py", line 197, in __init__
super().__init__(command_executor=executor, options=options, keep_alive=True)
File "/home/ubuntu/.local/share/virtualenvs/tool-lKp4P5xD/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 290, in __init__
self.start_session(capabilities, browser_profile)
File "/home/ubuntu/.local/share/virtualenvs/tool-lKp4P5xD/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 383, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/ubuntu/.local/share/virtualenvs/tool-lKp4P5xD/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 450, in execute
self.error_handler.check_response(response)
File "/home/ubuntu/.local/share/virtualenvs/tool-lKp4P5xD/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 249, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Failed to read marionette port
I got the problem.
In my opinion, I think it is a communication problem between Geckodriver and Firefox.
But I don't know how to fix it.
Additionally, I already tried using 'pyvirtualdisplay' or 'xvfb'.
But the result is same.
(https://azevedorafaela.com/tag/headless-browser-in-ubuntu/)
I found another way.
I read this post.
https://github.com/SeleniumHQ/selenium/issues/10813
I think it is a problem when you install Firefox on Ubuntu Snap package manager.
It works on Ubuntu 20.04.
Because Snap is not default package manager on that version.
It could be another solution to remove Snap on Ubuntu 22.04
so today I went to run a program I made in python 3, but it kept crashing when it tried to open chromium. I haven't updated python or the chromedriver.exe so it was strange why it didn't work.
Current version of chrome driver I have installed: 74.0.3729.6
I have tried the following things but it didn't work:
Updating to the latest version of the chrome driver (v77.0.3865.10)
Downgrading to v73 of the chrome driver
Nothing has worked. Could someone please assist me.
I made this test program with just it launching the chrome browser:
main.py
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(options=options)
driver.create_options()
driver.maximize_window()
crash message:
DevTools listening on ws://127.0.0.1:50904/devtools/browser/c1dc7138-e0cb-4ce4-a
561-56588f5ffd26
Traceback (most recent call last):
File "main.py", line 5, in <module>
driver = webdriver.Chrome(options=options)
File "C:\Users\myUser\AppData\Local\Programs\Python\Python37-32\lib\site-packages
\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "C:\Users\myUser\AppData\Local\Programs\Python\Python37-32\lib\site-packages
\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Users\myUser\AppData\Local\Programs\Python\Python37-32\lib\site-packages
\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Users\myUser\AppData\Local\Programs\Python\Python37-32\lib\site-packages
\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\myUser\AppData\Local\Programs\Python\Python37-32\lib\site-packages
\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not crea
ted: This version of ChromeDriver only supports Chrome version 74
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57
e9-refs/branch-heads/3729#{#29}),platform=Windows NT 6.3.9600 x86_64)
The version of both Chrome (or Chromium) and Chromedriver must coincide.
As you can see in the error, your current Chromedriver only works with Chromium version 74.
Check the version of you Chromium and install the corresponding version of Chromedriver. To do this you can run the following command in a terminal.
apt-cache policy chromium
I am using geckodriver for Firefox and I'm running Python 3 with Selenium. This is my code in the file script.py:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com')
Once I run it with python3 script.py with my terminal, it launches Firefox but simply doesn't load the page, nor does it insert the link in the address bar. My geckodriver is installed at this location: /usr/local/bin/geckodriver.
After waiting for about a minute, I get the following error:
Traceback (most recent call last):
File "script.py", line 3, in <module>
browser = webdriver.Firefox()
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 154, in __init__
keep_alive=True)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 151, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 240, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
I already searched a lot for fixes but none of them worked.
After I realized, I was using Firefox ESR instead of the normal one, I switched to the normal Firefox version and faced this problem:
Traceback (most recent call last):
File "script.py", line 3, in <module>
driver = webdriver.Firefox(executable_path=r'/root/Downloads/firefox-56.0.1/firefox/firefox')
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 144, in __init__
self.service.start()
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 102, in start
raise WebDriverException("Can not connect to the Service %s" % self.path)
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /root/Downloads/firefox-56.0.1/firefox/firefox
Which version of Selenium/Geckodriver/Browser are you using?
Anyway, do you set the executable_path:
driver = webdriver.Firefox(executable_path=r'pathTo/geckodriver')
?
EDIT
If you are using:
Selenium version = 3.6.0
Geckodriver version = 0.19.0
Firefox version = 52.4.0
Your problem is Firefox. From GeckoDriver releases, is recommended Firefox 55.0 (and greater).
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
When on windows I want to give a try with the webdriver extension for selenium, I get an error. I installed the python bindings like on the installation page. And then I try to to just the following:
from selenium import webdriver
browser = webdriver.Firefox()
Firefox (15.0) is opened and after that an exception occurs:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\selenium-2.32.0-py2.7.egg\selenium\webdriver\firefox\webdriver.py", line 62, in __init__
desired_capabilities=capabilities)
File "C:\Python27\lib\site-packages\selenium-2.32.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 72, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Python27\lib\site-packages\selenium-2.32.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 114, in start_session
'desiredCapabilities': desired_capabilities,
File "C:\Python27\lib\site-packages\selenium-2.32.0-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 165, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.32.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 152, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: ''
I tried the same without webdriver, with the 1.0 api and then I can open Firefox and go to a specific page, but with the webdriver case I do not have any luck. Has anyone a clue what I do wrong ?
My OS is Windos 7, 64 bit version.
I also tried the same with the latest python bindings from version control, but that has the same error.