I require this node module selenium-webdriver/chrome to run my script in headless.
I am unable to find this module on https://www.npmjs.com.
From where else I can find this module?
var chrome = require('selenium-webdriver/chrome');
var driver = new webdriver.Builder()
.forBrowser('chrome')
.setChromeOptions(new chrome.Options().headless().windowSize(screen))
.build();
As Evyatar pointed out to you, the module you're looking for is selenium-webdriver and not selenium-webdriver/chrome, so you're to install npm install selenium-webdriver --save then you require selenium-webdriver.
Example:
const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const firefox = require('selenium-webdriver/firefox');
let driver = new webdriver.Builder()
.forBrowser('firefox')
.setChromeOptions(/* ... */)
.setFirefoxOptions(/* ... */)
.build();
This should solve your problem.
You can get to read more on the usage of selenium-webdriver
Related
When trying to run Selenium on Windows 10 Edge Chrome version 80, I am getting the following error:
Z:\node_modules\selenium-webdriver\lib\promise.js:2626
throw error;
^
WebDriverError: Unknown error
at parseHttpResponse (Z:\node_modules\selenium-webdriver\lib\http.js:536:11)
at Z:\node_modules\selenium-webdriver\lib\http.js:441:30
at processTicksAndRejections (internal/process/task_queues.js:97:5)
From: Task: WebDriver.createSession()
[...]
I am building it like this:
const webdriver = require('selenium-webdriver'),
edge = require('selenium-webdriver/edge');
var service = new edge.ServiceBuilder()
.setPort(55555)
.build();
var options = new edge.Options();
driver = edge.Driver.createSession(options, service);
Does anyone see why this might be happening? Has anyone had success building the driver using the javascript bindings and can share how they were able to do that?
I'm confident that I have the correct version of the Microsoft Edge Driver
/* In another command prompt window enter the command:
msedgedriver.exe --verbose
Then run the script as normal in the other command prompt
*/
const Selenium = require("selenium-webdriver");
const BROWSER_NAME = Selenium.Browser.EDGE;
const builder = new Selenium.Builder().forBrowser(BROWSER_NAME)
.withCapabilities({
"browserName": 'MicrosoftEdge',
"browserVersion": '81.0',
"platformName": 'Windows 10',
'ms:edgeChromium': true
}).usingServer('http://localhost:9515')
const driver = builder.build()
I have the following Javascript code that launches Chrome with the path to Chrome specified by the PATH environment variable.
let driver = await new Builder()
.forBrowser('chrome')
.build();
How can I launch Chrome with a specific path to Chrome? I would love something like this:
let driver = await new Builder()
.forBrowser('chrome')
.withPath('C:\\temp\\chrome.exe')
.build();
You'll probably need to use ChromeOptions to setup custom chrome.exe
const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const builder = new webdriver.Builder().forBrowser('chrome');
const chromeOptions = new chrome.Options();
chromeOptions.setChromeBinaryPath("/path/to/chrome.exe");
builder.setChromeOptions(chromeOptions);
const driver = builder.build();
Documentation for chrome.Options
We use PhantomJS as simple test runner like this:
phantomjs path/to/test.js
Is there a similar way with headless chrome?
Selenium WebDriver is a NPM package that can help you to run a headless browser.
Try the below example:
const chrome = require('selenium-webdriver/chrome');
const {Builder, By, Key, until} = require('selenium-webdriver');
const width = 640;
const height = 480;
let driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(
new chrome.Options().headless().windowSize({width, height}))
.build();
driver.get('http://www.google.com/ncr')
.then(_ =>
driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN))
.then(_ => driver.wait(until.titleIs('webdriver - Google Search'), 1000))
.then(
_ => driver.quit(),
e => driver.quit().then(() => { throw e; }));
According to the API, the driver methods return Promises and, as a result, can be called using the async/await syntax:
const chrome = require('selenium-webdriver/chrome');
const {Builder, By, Key, until} = require('selenium-webdriver');
async function test() {
const width = 640;
const height = 480;
let driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(
new chrome.Options().headless().windowSize({width, height}))
.build();
await driver.get('http://www.google.com/ncr')
await driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN))
await driver.wait(until.titleIs('webdriver - Google Search'), 1000))
await driver.quit()
}
test();
You can run you tests with Karma and headless Chrome. Here's a guide for doing that.
Puppeteer's also pretty well-equipped to do this. Here's a guide for that too.
You're looking for Puppeteer, an API for headless Chrome/Chromium.
Once you have your script (the docs are good), you can run it with node script.js.
as explained on developers.google you have option of using Node or Selenium, in either case you will have to re-write part of your test cases to use the new api instead of the phantomjs api.
The following options are based on Node:
Puppeteer
I suggest you to head over to their github page for installation and usage instructions.
Your test case should be written using the Puppetteer api and run with:
node path/to/test.js
There is a list of puppeteer resources, you can use puppeteer with jest, mocha, angular, e2e..
chrome-remote-interface
library
chrome-remote-interface is a lower-level library than Puppeteer's API. I recommend it if you want to be close to the metal and use the DevTools protocol directly.
You need to start chrome in headless mode or use lighthouse or nodejs:
node --inspect=9222 path/to/test.js
write your javascript test case using the chrome DevTools protocol and follow the instructions on their github page to install and run your tests.
The other option is using Selenium configured to run headless Chrome. The following options use Selenium:
ChromeDriver
Webdriver
I have my selenium standalone server set up as running with the IEDriver as a parameter using the selenium-standalone start --drivers.ie.arch=ia32 configuration.
I'm getting the following error when I try to run my internet explorer selenium test:
index.js:673
throw new Error('Do not know how to build driver: ' + browser
^
Error: Do not know how to build driver: IE; did you forget to call
usingServer(url)?
Yet I have the correct server listed in my code:
const {Builder, By, Key, until} = require('selenium-webdriver');
driver = await new webdriver.Builder().forBrowser('IE').usingServer('http://localhost:4444/wd/hub').build();
I have also tried this:
let driver = new webdriver.Builder()
.forBrowser('internet explorer')
.usingServer('http://localhost:4444/wd/hub')
.build();
but neither of these work and I get the same error message...
Any help would be appreciated!
https://github.com/SeleniumHQ/selenium/wiki/Grid2 Just read this article, you are missing something. And run your IE tests under administrator for future.
driver = new webdriver.Builder().
usingServer("http://localhost:4444/wd/hub").
withCapabilities(webdriver.Capabilities.ie()).
build();
It works for me.
I am currently trying to run some tests made with webdriverjs and chromedriver but they need microphone permissions.
This is the popup that shows up:
I have tried:
chromedriver.start(['--disable-popup-blocking']);
driver = new Webdriver.Builder()
.withCapabilities(Webdriver.Capabilities.chrome())
.build();
but it didn't work.
I also tried
driver.wait(Until.alertIsPresent(), config.TIMEOUT, 'Alert did not show up');
driver.switchTo().alert().accept();
it did not work either! I guess that this is not an ordinary alert.
Useful links:
Chrome startup arguments list
Chrome options for java and ruby
Chromedriver github
How do I give them permissions programmatically?
Is there any flag or some other way around this?
A little late but pasting how to do this here for others looking for the same.
const webdriver = require('selenium-webdriver'), By = webdriver.By, until = webdriver.until,Builder= webdriver.Builder;
var chrome = require('selenium-webdriver/chrome');
var chromeOptions = new chrome.Options()
.addArguments('allow-file-access-from-files')
.addArguments('use-fake-device-for-media-stream')
.addArguments('use-fake-ui-for-media-stream');
var driver = new webdriver.Builder()
.forBrowser('chrome')
.setChromeOptions(chromeOptions);
driver = driver.build();
A fresh profile is loaded each time you run selenium, hence changes you make to the preferences and website permissions are not preserved between sessions. To amend this we need to tell selenium which profile to load.
Step 1. Find your Chrome preferences file: www.forensicswiki.org/wiki/Google_Chrome#Configuration
Step 2. Copy the folder Default somewhere. I will assume it is copied to /some/path/allow-mic/Default.
Alternative Step 3 (this is easier):
Before copying Default visit localhost:1337 with Chrome and set mic to always allow.
Step 3. Edit allow-mic/Default/Preferences, find the tags "profile", "content_settings" and "exceptions" within each other and add
"media_stream_mic":{"http://localhost:1337,*":
{"last_used":1470931206,
"setting":1} },
to "exceptions". You should end up with something like:
...
"profile":{
...
"content_settings": {
...
"exceptions": {
...
"media_stream_mic":{"http://localhost:1337,*":
{"last_used":1470931206,
"setting":1} },
...
},
},
},
...
Step 4:
Configure selenium to use the edited preferences:
var chromedriver = require('chromedriver');
var Webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');
var opts = new chrome.Options();
opts.addArguments("user-data-dir=/some/path/allow-camera");
var driver = new chrome.Driver(opts);
You can check that the correct set of preferences (Profile path) are in use by opening chrome://version/.
For those using Python, this worked for me:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--use-fake-ui-for-media-stream")
driver = webdriver.Chrome(chrome_options=chrome_options)
You can whitelist a url for audio-capture by providing chromedriver with the hardware.audio_capture_allowed_urls preference.
...
chrome_options = Options()
prefs = {"hardware.audio_capture_allowed_urls" : ["example.org"]}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
Similarly, for use in Splinter
from splinter import Browser
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--use-fake-ui-for-media-stream")
Browser('chrome', ** {'executable_path':'chromedriver'},options=chrome_options)
If you want to use the microphone,
chrome_options = Options()
chrome_options.add_experimental_option('prefs',{'profile.default_content_setting_values.media_stream_mic':1})
driver = webdriver.Chrome(path,chrome_options=chrome_options)