Its been a while I'm trying to figure out a way to automatically accept SSL certs. But unfortunately no luck. So, here is the case, I'm working on selenium tests. And, every time when I run the test on chrome, a small pop-up appears asking to select a certificate.
I tried this in python: How to deal with certificates using Selenium?
I also tried (in javascript):
var options = new chrome.Options();
options.addArguments("--ignore-certificate-errors")
But it doesn't seems to work!
In firefox, there is an option to automatically selects certs. Is there any way in selenium or in chrome settings which automatically selects the certs? Will ENTER/RETURN keys in selenium work?
EDITED: Below is my code. Is this the right way to use?
var launch = function(){
var options = new chrome.Options();
options.addArguments("--test-type");
/* Also tried options.addArguments(“--ignore-certificate-errors")
*/
var driver = new webdriver.Builder()
.usingServer('http://127.0.0.1:4444/wd/hub')
.setChromeOptions(options)
.build();
driver.get(url)
}
P.S Here, I'm using JavaScript.
ACCEPT_SSL_CERTS is one of the browser's desired capability that tells the browser to accept / deny ssl certificates by default.
below is a sample code for accepting SSL certificates in chrome:
DesiredCapabilities cap=DesiredCapabilities.chrome();
// Set ACCEPT_SSL_CERTS variable to true
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
// Set the driver path
System.setProperty("webdriver.chrome.driver","Chrome driver path");
// Open browser with capability
WebDriver driver=new ChromeDriver(cap);
It is major problem with Chrome versions above v.80.
I am currently using version 84.0.4147.105 which won't accept or ignore SSL certificate.
The one thing to do is to downgrade Chrome version below v.80, especially for those of you who are building test cases in local Host applications.
Example using Selenium-grid and Chrome
const { Builder, until, By } = require("selenium-webdriver");
const capabilities = {
browserName: "chrome",
acceptInsecureCerts: true,
};
// alternative require `Capabilities` and do like this:
// const capabilities = Capabilities.chrome().setAcceptInsecureCerts(true);
try {
const driver = await new Builder()
.usingServer("http://localhost:4444/wd/hub")
.withCapabilities(capabilities)
.build();
await driver.get("https://frontend");
// tests
const el = await driver.wait(until.elementLocated(By.id("root")), 2000);
await driver.wait(until.elementIsVisible(el), 2000);
expect(el).not.toBeNull();
// tear down
await driver.quit();
} catch (err) {
throw new Error(err);
}
chrome_options = ChromeOptions()
chrome_options.add_argument('--lang='+language)
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--allow-running-insecure-content')
chrome_options.add_argument('--disable-web-security')
chrome_options.add_experimental_option('useAutomationExtension', False)
if headless :
chrome_options.set_headless()
if remote :
self.driver = webdriver.Remote(command_executor=wd,desired_capabilities=chrome_options.to_capabilities(),options=chrome_options)
else:
self.driver = webdriver.Chrome(desired_capabilities=chrome_options.to_capabilities(),options=chrome_options)
Related
The title says it all: I am wondering whether it is possible to interact with the Firefox console upon starting Firefox in headless mode.
More generally, I'd settle for some way of accessing it programmatically, in scripts.
What I've tried:
So far I've been playing with the Javascript bindings to Selenium without success:
Starting Firefox with the -devtools option from Selenium does opn the dev tools, but I then cannot send key combinations that will switch me to the actual console, or in fact interact from my .js script with the open devtools window in any way.
Edit
In response to the first comment below: this answer does not seem to help. The console is not opened when I send CTRL+SHIFT+k to the body tag of google.com:
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var firefox = require('selenium-webdriver/firefox');
var inpt = require('selenium-webdriver/lib/input');
var options = new firefox.Options();
var driver = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
(async function(){
await driver.get('https://google.com');
var bdy = await driver.findElement(By.id('gsr'));
await bdy.sendKeys(inpt.Key.CONTROL + inpt.Key.SHIFT + 'k');
})();
This opens the page (google.com) and returns no errors, but there's no console anywhere.
For good measure: sending just inpt.Key.SHIFT + 'k' does enter a capital 'K' in the Google search field, so I know the keys are referenced correctly.
Also, sending just 'k' enters a small 'k' in the search field. It's only the three-key combo that does not work.
2nd edit:
I take it back: the newer answer does work, precisely as-is (I switched to Python from node).
The comment below by Karthik does resolve the matter, but I would like to summarize here and document working solutions that automate Firefox-Web-Console access.
The point of the answer I linked to above (in my 2nd edit) is that in order to have full access to the Firefox browser key controls one must
first switch Firefox context to chrome (from the default content context)
direct the automated browser driver to locate the element carrying id tabbrowser-tabs
send the key combo (in this case Ctrl+Shift+k) to that element.
Concrete working solutions:
Python
The script is
from selenium.webdriver import Firefox, DesiredCapabilities, FirefoxProfile
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
import time
options = Options()
webdriver = Firefox(options=options)
webdriver.get("https://google.com")
try:
time.sleep(3)
with webdriver.context(webdriver.CONTEXT_CHROME):
console = webdriver.find_element(By.ID, "tabbrowser-tabs")
console.send_keys(Keys.LEFT_CONTROL + Keys.LEFT_SHIFT + 'k')
except:
pass
Run with python <path-to-script> it opens a Firefox window displaying google.com and the console at the bottom.
Javascript
Here the full script is
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var firefox = require('selenium-webdriver/firefox');
var inpt = require('selenium-webdriver/lib/input');
var options = new firefox.Options();
var driver = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
(async function(){
await driver.get('https://google.com');
await driver.setContext("chrome");
var tabs = await driver.findElement(By.id('tabbrowser-tabs'));
await tabs.sendKeys(inpt.Key.CONTROL + inpt.Key.SHIFT + 'k');
})();
Run with node <path-to-script> it achieves the same effect as above: a Firefox window open on google.com, with the console open at the bottom.
I'm trying to set some specific capabilities for a Selenium Webdriver.
In this example i want to change a capability of the webdriver for the Firefox browser. I'm trying to this in Javascript.
this.driver = new selenium.Builder().forBrowser('firefox').build();
I tried things like calling .withCapabilities() but it is not working as i expected it and crashes the program.
In specific i want to set the 'acceptSslCerts' capability to true because it is false in default.
Does anybody have an idea on how to this?
I'm not able to find anything in the API reference or on the internet.
This works fine for me, to set CORS in my chrome browser.
const { Builder, By, Key, until } = require('selenium-webdriver');
const capabilities = {
'chromeOptions': {
'args': ['--disable-web-security', '--user-date-dir']
}
}
let driver = new Builder().withCapabilities(capabilities).forBrowser('chrome').build();
For firefox, through FirefoxProfile, you can accept SSL certificate.
e.g FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setAcceptUntrustedCertificates(true)
For googlechrome, through DesiredCapability, you can set the browser property.
Because Webdriver waits for the entire page to load before going on to the next line, I want to disable images will speed things up when the network is slow.
This is the example js file in Selenium Webdriver's website:
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var driver = new webdriver.Builder()
.forBrowser('chrome')
.build();
driver.get('http://www.google.com/ncr');
driver.findElement(By.name('q')).sendKeys('webdriver');
driver.findElement(By.name('btnG')).click();
driver.wait(until.titleIs('webdriver - Google Search'), 1000);
driver.quit();
How can I disable image in my code?
I have search google for this question, I only get this solution in Python: Disable images in Selenium Python.
On a high level, I see some solutions:
set profile.managed_default_content_settings.images to 2 (I can't find the corresponding chromedriver documentation, but you can google it).
Set up a proxy. Connect to your page via a proxy that returns empty data when asking for an image file.
load the browser with a browser plugin that does this for you. Something (a bit like ad-blocked works) might be available already. (con: browser-specific solution)
Here I give you code for not loading image.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option( "prefs", {'profile.managed_default_content_settings.images': 2})
driver = webdriver.Chrome("chromedriver.exe",chrome_options=chrome_options)
You can pass an optionsobject to WebdriverJS' Builder that disables images:
{
prefs: {
profile: {
managed_default_content_settings: {
images: 2
}
}
}
}
The complete example is:
const chromeDesktop = {
prefs: {
profile: {
managed_default_content_settings: {
images: 2
}
}
}
};
const { By, Builder, until } = require('selenium-webdriver');
const driver = new Builder().withCapabilities(chromeDesktop).build();
This definitely worked for me.
import { Options } from 'selenium-webdriver/chrome';
const options = new Options();
options.setUserPreferences({
'profile.managed_default_content_settings.images': 2, //disable loading
});
The objective is to hit a local version of GhostDriver, by leveraging the configuration of its path, that the Capabilities allow us to set:
https://github.com/detro/ghostdriver/blob/master/README.md#what-extra-webdriver-capabilities-ghostdriver-offers
When running a test with Selenium webdriverjs like the trivial example:
var webdriver = require('selenium-webdriver');
var capabilities = webdriver.Capabilities.phantomjs();
capabilities.set('phantomjs.ghostdriver.path',
'/Users/xyz/ghostdriver-master/src/main.js');
var driver = new webdriver.Builder().
withCapabilities(capabilities).
build();
driver.get('http://www.google.com');
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
driver.findElement(webdriver.By.name('btnG')).click();
driver.wait(function() {
return driver.getTitle().then(function(title) {
console.log(title);
});
}, 1000);
driver.quit();
I am not able to hit my local version of GhostDriver. It just hits the default version which is baked into phantomjs.
However, if I use WebdriverIO using the same Capability definition:
var webdriverio = require('webdriverio');
var options = {
desiredCapabilities: {
'browserName': 'phantomjs',
'phantomjs.ghostdriver.path': '/Users/xyz/ghostdriver-master/src/main.js'
},
port: 4444
};
webdriverio
.remote(options)
.init()
.url('http://www.google.com')
.title(function(err, res) {
console.log(res.value);
})
.end();
I am able to hit my local version of GhostDriver.
After looking at the documentation here:
https://github.com/detro/ghostdriver/blob/master/README.md#what-extra-webdriver-capabilities-ghostdriver-offers
and other pages with similar issues, I was not able to find anything that worked.
versions used:
selenium-server-standalone v2.51.0
selenium-webdriver v2.48.2
webdriverio v4.0.2
phantomjs v2.1.1
Has anybody got an idea of how to get this working?
With the below code block it opens a chrome browser fine it just won't full screen the browser using F11. i used to use C# and selenium and that worked fine using this method on chrome and different browsers. It finds the element 'body' but then does not send the key press. Am I doing something wrong here that i should be requiring some other library?
the documentation for webdriverjs is pathetic and there is very few examples, I am seriously considering dumping it for something else possibly python.
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('https://www.google.co.uk/');
driver.wait(function () {
return driver.getTitle().then(function (title) {
return title === 'Google';
});
}, 1000);
driver.findElement(webdriver.By.xpath('/html/body')).sendKeys("F11");
why are we doing this. we are developing a website that will change depending on size 800x600 + with and without the toolbar depending on how the screen is used different items will be displayed. i can maximise the window using,
driver.manage().window().maximize();
This however still leaves the toolbar present and doesn't act as if the user has pressed the F11 key.
it tooks some time to find it but you should have all the Keys in webdriver.Key
driver.findElement(webdriver.By.xpath('/html/body')).sendKeys(webdriver.Key.F11);
Hope it helps!
A co-worker has just discovered that it works well in C# with:
Driver.Instance.Manage().Window.FullScreen();
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).send_keys(Keys.F11).perform()
I use a similar command to toggle JS via Firefox's NoScript add-on
edit: I just tested, and it works!
This should work for you:
driver.findElement(webdriver.By.xpath('/html/body')).sendKeys(Keys.F11);
You can maximise the window using:
driver.manage().window().maximize();
For me the one emulating correctly the F11 on startup is:
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-fullscreen");
WebDriver driver = new ChromeDriver(options);
Or if the kiosk mode is preferred:
ChromeOptions options = new ChromeOptions();
options.addArguments("--kiosk");
WebDriver driver = new ChromeDriver(options);