So, I'm quite curious about this one.
I've been trying TDD with Django, and took an exercise out of Harry J.W. Percival's book, as follows:
from selenium import webdriver
import unittest
class NewVisitorTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Chrome()
def tearDown(self):
self.browser.quit()
def test_can_start_list_and_retrieve_later(self):
self.browser.get('http://localhost:8000')
self.browser.implicitly_wait(3)
self.assertIn('Django', self.browser.title)
Seems simple enough.
Except I get this failing test:
error: [Errno 10061] No connection could be made because the target machine actively refused it
So I believe I have some kind of Firewall up (which I'd be thankful if you could direct me to). The most curious thing, however is the following:
If I run the script with this one line omitted, however: self.browser.implicitly_wait(3)
The test curiously passes.
When I fail the test, Django says the following:
Not Found: /favicon.ico
[22/Feb/2016 09:34:38] "GET /favicon.ico HTTP/1.1" 404 1940
If this is of any relevance:
OS Is Windows 10
IDE is PyCharm community (so Django is being run in powershell)
Testing framework is Unittest.py
Chrome requires a specific driver that matches the Chrome version which is installed on your machine:
self.browser = webdriver.Chrome("/path/to/chromedriver")
Download the chromedrive and copy it to "/path/to/chromedriver" from https://sites.google.com/a/chromium.org/chromedriver/downloads
Note: For Firefox it's OK to use webdriver.Firefox()
Related
I am attempting to automate the push of a backup file from a (local) headless data server, to a (remote) secure backup server - which [for reasons] (currently) needs to be done via a web page.
I have written a Selenium/Python script which works well, but ignores ALL certificate errors - which, in this case, is NOT the desired behaviour.
Platform: Raspberry Pi 3B / Raspbian 10
Selenium: v3.141.0 (from Raspbian repo)
Geckdriver: v0.29 (built from github release source with rust v1.50)
...Installed with: cp /path/to/geckodriver-0.29.0/target/debug/geckodriver /usr/local/bin
Firefox: v78.7.0esr (from Raspbian repo)
Most people (in my searches) seem to have the exact /opposite/ problem - IE. they want to DISable the security mechanism ...But inverting the (literally True/False) logic in those many different solutions has NOT led me to a solution which allows me to ENable cert checking.
Here is some (abbreviated and annotated) example code, with one of my (many) attempts:
#!/bin/python3
from selenium import webdriver # selenium instance
from selenium.webdriver.firefox.options import Options # headless
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities # Reject bad SSL
# Set headless mode
opts = Options()
opts.headless = True
# Enforce certificate checking - [this fails]
caps = DesiredCapabilities.FIREFOX
print(caps) # {'browserName': 'firefox', 'marionette': True, 'acceptInsecureCerts': True}
caps['acceptInsecureCerts'] = False
print(caps) # {'browserName': 'firefox', 'marionette': True, 'acceptInsecureCerts': False}
driver = webdriver.Firefox(capabilities=caps, options=opts)
# LOG FILE ENTRY APPEARS in geckodriver.log:
# "Marionette WARN TLS certificate errors will be ignored for this session"
driver.get("https://wrong.host.badssl.com")
# <<perform upload here>>
driver.save_screenshot("test.png") # Page was NOT blocked! :(
driver.close()
My question is:
What modification(s) can be made to this code such that the <<upload>> will happen if (and only if) the SSL/TLS certificates are valid?
Extra kudos for an additional line of code to display a reason {"self-signed cert", "cert revoked", etc} if the <<upload>> is blocked.
from selenium import webdriver
capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities['acceptInsecureCerts'] = False
capabilities['marionette'] = True
driver = webdriver.Firefox(desired_capabilities=capabilities)
# LOG FILE ENTRY APPEARS in geckodriver.log:
# "Marionette WARN TLS certificate errors will be ignored for this session"
driver.get("https://wrong.host.badssl.com")
You should use desiredcapability , use the above code
I'm trying to follow a tutorial about Selenium, http://selenium-python.readthedocs.io/getting-started.html. I've downloaded the latest version of geckodriver and copied it to /usr/local/bin. However, when I try
from selenium import webdriver
driver = webdriver.Firefox()
I get the following error message:
Traceback (most recent call last):
File "/Users/kurtpeek/Documents/Scratch/selenium_getting_started.py", line 4, in <module>
driver = webdriver.Firefox()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 152, in __init__
keep_alive=True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 98, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 188, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities
[Finished in 1.2s with exit code 1]
From https://github.com/SeleniumHQ/selenium/issues/3884, it seems like other users are experiencing similar issues, but the Selenium team is unable to reproduce it. How can I get Selenium working with Firefox? (It does work with chromedriver and a webdriver.Chrome() instance, so I suspect this might be a bug in Selenium).
Updating Firefox and Selenium solved it for me. I don't pretend to have an explanation for the root cause however.
Updated Firefox 48 → 53
Updated to Selenium 3.4.1
I also reinstalled/updated Geckodriver using Homebrew and explicitly used it as an executable for Selenium WebDriver, but it turned out that it wasn't necessary to mitigate the "Unable to find matching set of capabilities" error.
I had this same issue, and the problem was related to using Firefox ESR (I'm on Debian). To be more specific, I'm on Debian 10 using 64-bit Firefox 68.11.0esr, python3.7, selenium 3.141.0, and geckodriver 0.27.0.
Here's the standard example I used that failed:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://google.com")
As recommended in this answer, I changed:
browser = webdriver.Firefox()
to
browser = webdriver.Firefox(firefox_binary="/usr/bin/firefox-esr")
and it worked.
If you don't know the path to firefox-esr, you can run sudo find / -name firefox-esr on the command line. Several should come up.
for me it was enough to just upgrade FF
Mac user here.
I fixed this issue by making sure Firefox is named "Firefox" and in the "Applications" folder. I had called it "Firefox 58" before (I have multiple versions).
Just sharing my success case here
Note: Remember the Architecture matters here, Window 64/32 or Linux 64/32. Make sure you download the right 64/32 bit Selenium Webdriver, 64/32 Geckodriver.
My configuration was as follows:
Linux: Centos 7 64bit, Window 7 64bit
Firefox: 52.0.3
Selenium Webdriver: 3.4.0 (Windows), 3.8.1 (Linux Centos)
GeckoDriver: v0.16.0 (Windows), v0.17.0 (Linux Centos)
Working Code (Without Proxy Settings)
System.setProperty("webdriver.gecko.driver", "/home/seleniumproject/geckodrivers/linux/v0.17/geckodriver");
ProfilesIni ini = new ProfilesIni();
// Change the profile name to your own. The profile name can
// be found under .mozilla folder ~/.mozilla/firefox/profile.
// See you profile.ini for the default profile name
FirefoxProfile profile = ini.getProfile("default");
DesiredCapabilities cap = new DesiredCapabilities();
cap.setAcceptInsecureCerts(true);
FirefoxBinary firefoxBinary = new FirefoxBinary();
GeckoDriverService service =new GeckoDriverService.Builder(firefoxBinary)
.usingDriverExecutable(new File("/home/seleniumproject/geckodrivers/linux/v0.17/geckodriver"))
.usingAnyFreePort()
.build();
try {
service.start();
} catch (IOException e) {
e.printStackTrace();
}
FirefoxOptions options = new FirefoxOptions().setBinary(firefoxBinary).setProfile(profile).addCapabilities(cap);
driver = new FirefoxDriver(options);
driver.get("https://www.google.com");
System.out.println("Life Title -> " + driver.getTitle());
driver.close();
Working Code (With Proxy Settings)
System.setProperty("webdriver.gecko.driver", "/home/seleniumproject/geckodrivers/linux/v0.17/geckodriver");
String PROXY = "my-proxy.co.jp";
int PORT = 8301;
ProfilesIni ini = new ProfilesIni();
// Change the profile name to your own. The profile name can
// be found under .mozilla folder ~/.mozilla/firefox/profile.
// See you profile.ini for the default profile name
FirefoxProfile profile = ini.getProfile("default");
com.google.gson.JsonObject json = new com.google.gson.JsonObject();
json.addProperty("proxyType", "manual");
json.addProperty("httpProxy", PROXY);
json.addProperty("httpProxyPort", PORT);
json.addProperty("sslProxy", PROXY);
json.addProperty("sslProxyPort", PORT);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setAcceptInsecureCerts(true);
cap.setCapability("proxy", json);
FirefoxBinary firefoxBinary = new FirefoxBinary();
GeckoDriverService service =new GeckoDriverService.Builder(firefoxBinary)
.usingDriverExecutable(new File("/home/seleniumproject/geckodrivers/linux/v0.17/geckodriver"))
.usingAnyFreePort()
.usingAnyFreePort()
.build();
try {
service.start();
} catch (IOException e) {
e.printStackTrace();
}
FirefoxOptions options = new FirefoxOptions().setBinary(firefoxBinary).setProfile(profile).addCapabilities(cap);
driver = new FirefoxDriver(options);
driver.get("https://www.google.com");
System.out.println("Life Title -> " + driver.getTitle());
driver.close();
In my case, I only have Firefox Developer Edition but still throw same error.
After installing a standard Firefox version, it solves.
I had the same issue. My geckodriver was 32 bit and fireFox was 64. Resolved by updating geckodriver to 64 bit.
I had exactly the same issue when i was using selenium firefox()
>> webdriver.Firefox()
it was not working : throwing error like "Unable to find a matching set of capabilities"
Then i installed geckodriver.exe and that put that .exe file inside the both directory
C:\Users\<USER-NAME>\AppData\Local\Programs\Python\Python36\Scripts
and
C:\Users\<USER-NAME>\AppData\Local\Programs\Python\Python36\
and set these two paths in the environment setting
then it started working
Here's the solution that solved it for me. Don't overlook this point: make sure you're using the correct 32/64 bit version of the binaries - it should be uniform - e.g. if the firefox is 64bit, so must be the geckodriver.
Got the same error on a droplet at DigitalOcean - FireFox was not installed . Stack trace of error was as seen below -
exception_class
<class 'selenium.common.exceptions.SessionNotCreatedException'>
json
<module 'json' from '/usr/lib/python3.5/json/__init__.py'>
message
'Unable to find a matching set of capabilities'
response
{'status': 500,
'value': '{"value":{"error":"session not created","message":"Unable to find a '
'matching set of capabilities","stacktrace":""}}'}
screen
None
self
<selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x7f428e3f10f0>
stacktrace
None
status
'session not created'
value
{'error': 'session not created',
'message': 'Unable to find a matching set of capabilities',
'stacktrace': ''}
value_json
('{"value":{"error":"session not created","message":"Unable to find a matching '
'set of capabilities","stacktrace":""}}')
It seems like different workarounds are seem to make the error go away. After ensuring you have downloaded and installed the 64bit versions for Firefox and geckodriver.exe, update the PATH with the location of the geckodriver.exe. What may also help before running the script, launch the geckodriver.exe which opens a cmd like window. Now if you run the py script, you shouldn't run into the error below:
selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
I'm doing a testing unit that requires the tests to be run via web browser. I'm using Ubuntu VPS 14 with LAMP stack installed, mod_wsgi, selenium 2.44 and PhantomJS 1.9. I'm testing with the very simple code first:
from flask import Flask, request
from selenium import webdriver
app = Flask(__name__)
app.debug = True
#app.route("/test")
def test():
url = "http://www.google.com"
driver = webdriver.PhantomJS('./phantomjs')
driver.get(url)
result = driver.page_source
driver.close()
return result
if __name__ == "__main__":
app.run()
The code runs very smoothly on my local Ubuntu, it prints out the google page when I connect to: 127.0.0.1:5000/test . On my Ubuntu VPS, my have my flask already setup and running. Now i use the same code to be the index file (supposed all configs are OK and 'hello world' runs), I have 500 Internal Server Error when connecting to http://xxx.xxx.xxx.xxx/test
Apache log sends out the following error:
... service_args=service_args, log_path=service_log_path File
"/usr/local/lib/python2.7/ddist-packages/selenium/webdriver/phantomjs/service.py",
line 53, in init
self._log = open(log_path, 'w') in > ignored Exception AttributeError: "'Service' object
has no attribute '_log'" in > ignored
I changed the log_path for phatomJS but still have the same problem. However, if I open python console, doing line by line as following:
from selenium import webdriver
br = webdriver.PhantomJS('./phantomjs')
....
I got no error. It took me the whole day to fin the problem but I could't be able to fix it. Any ideas how to solve this problem?
Figure out the problem, current phantomjs and init.py don't have enough permission to manipulate the service.py of ghostdriver. Here is the fix:
Add a new user: "add username", then set "add username sudo"
Login to the new user, make sure every command you run from now on starts with "sudo"
In your flask application where init.py resides, create "ghostdriver.log" and "phantomjs" which is an executable file.
Chmod both of them to 777 : init.py, ghostdriver.log and phantomjs
Set the custom config in init.py for phantomjs:
br = webdriver.PhantomJS(service_log_path='./ghostdriver.log', executable_path='./phantomjs')
That's it, now your selenium + flask + phantomjs is now working correctly.
I'm experimenting with creating a basic library extension for Robot Framework using Python, and I'm using PyCharm as the editor. For libraries imported directly code completion is working fine, but in this case I'm importing the Selenium2Library indirectly via a method:
def get_current_browser():
browser = BuiltIn().get_library_instance('Selenium2Library')._current_browser()
return browser
Which I call from other methods with something like
driver = get_current_browser()
This successfully grabs the webdriver browser instance from Robot Framework and lets me do as I please, but I don't get code hints when I go to edit a 'driver' variable. Is there way I can get hints in this scenario?
Here's the code in full:
from robot.libraries.BuiltIn import BuiltIn
from Selenium2Library.keywords.keywordgroup import KeywordGroup
import logging
def get_current_browser():
browser = BuiltIn().get_library_instance('Selenium2Library')._current_browser()
return browser
class MyLibrary(KeywordGroup):
def get_title_via_python(self):
driver = get_current_browser()
title = driver.title
logging.warn("checking title %s" % title)
return title
Try adding a docstring to your function to help PyCharm.
from selenium.webdriver import Remote # Remote imported only for code completion
def get_current_browser():
"""
:rtype: Remote
"""
browser = BuiltIn().get_library_instance('Selenium2Library')._current_browser()
return browser
More at http://www.jetbrains.com/pycharm/webhelp/type-hinting-in-pycharm.html
I'm trying to run Selenium's Firefox webdriver and am getting the error below. I can see that the response does not have a sessionId - the offending line is self.session_id = response['sessionId'] - but I don't know why. I've run this in the following ways and get the same error:
Cygwin, running nosetests
Cygwin directly
Windows, running nosetests
Windows directly
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\dev\tools\cygwin\home\207013288\dev\projects\scorpion\test\unit\test_
approve_workflows.py", line 27, in test_login
'password', userid='207013288', test=True)
File "C:\dev\tools\cygwin\home\207013288\dev\projects\scorpion\src\workflows.p
y", line 20, in login
browser = webdriver.Firefox()
File "C:\dev\sdks\Python33\lib\site-packages\selenium-2.32.0-py3.3.egg\seleniu
m\webdriver\firefox\webdriver.py", line 62, in __init__
desired_capabilities=capabilities)
File "C:\dev\sdks\Python33\lib\site-packages\selenium-2.32.0-py3.3.egg\seleniu
m\webdriver\remote\webdriver.py", line 72, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\dev\sdks\Python33\lib\site-packages\selenium-2.32.0-py3.3.egg\seleniu
m\webdriver\remote\webdriver.py", line 116, in start_session
self.session_id = response['sessionId']
nose.proxy.KeyError: 'sessionId'
-------------------- >> begin captured logging << --------------------
selenium.webdriver.remote.remote_connection: DEBUG: POST http://127.0.0.1:63801/
hub/session {"sessionId": null, "desiredCapabilities": {"version": "", "browserN
ame": "firefox", "platform": "ANY", "javascriptEnabled": true}}
--------------------- >> end captured logging << ---------------------
I haven't used Selenium before and I'm not sure where to go from here.
OK, first of all I'd like to say that I nearly pulled out all my hair trying to fix this.
My setup:
Python 2.7
Firefox 22.0
Selenium WebDriver 2.33
Windows XP (internet connection via proxy server)
Spotted my problem yet?
Solution:
Open the control Panel
Internet options
Connections
LAN Configuration
On this page you will probably see the details of your proxy server, complete with a checkbox: "Do not use proxy server for local addresses".
This checkbox is probably already selected (as it makes good sense to not use a proxy for local addresses).
Unfortunately, Selenium WebDriver appears to ignore this checkbox and it's value.
Checked or unchecked, it will detect your proxy server and apply it to all url's (or at least that's what it did in my case.)
Click the "Advanced" button, and manually enter "127.0.0.1" in the "Exceptions" box.
Save the change.
Re-run your test in web driver, hopefully you will no longer see error message that refers to: self.session_id = response['sessionId']
Things that didn't work for me:
Before finding this solution (with the aid of a more experienced engineer) I went through the instructions here:http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#advanceduserinteractions
.. for setting/changing/bypassing the proxy. Didn't get me the result I was looking for, though.
I also looked at this:
Implementing WebdriverBackedSelenium in Python
.. but it speaks of a older version of webdriver, and it suggests removing all the proxy settings system wide, which is an absolute non-starter (as far as I'm concerned).
MAC OSX SOLUTION
I'm using Python 2.7 and FireFox 48.0.2 and Chrome Versie 57.0.2987.98 (64-bit).
The error *self.session_id = response['sessionId']* for me was solved by going to System preferences -> Network -> Advanced in the Wifi Tab. -> Proxy's -> Turning "Automatic Proxydetection" on.
After changing this, the error no longer occurred.