Add options to selenium chrome browser using nodejs - node.js

I am using selenium with Node.js in this way
import {Builder, Browser, By, Key, until} from "selenium-webdriver";
let driver = await new Builder().forBrowser(Browser.CHROME).build();
I want to add chrome options in this way
const chrome = require('selenium-webdriver/chrome')
const options = new chrome.Options()
options.addArguments('--disable-dev-shm-usage')
options.addArguments('--no-sandbox')
options.addArguments('--headless')
let driver = await new Builder().forBrowser(Browser.CHROME).setChromeOptions(options).build();
But I can only use imports in my project, I can't use require. I get the following error because of this line
const chrome = require('selenium-webdriver/chrome')
require is not defined in ES module scope, you can use import instead
How can I import chrome instead of require it to add the option? I am importing a lot of module in my code, It will be really difficult to change them all to requires

You can refer to the file as "selenium-webdriver/chrome.js". I.e.:
import {Options} from "selenium-webdriver/chrome.js";
const options = new Options();
// use options as you always would...

Related

How to import safari webdriver from selenium npm package?

I am trying to load webpage in Safari via Webdriver in Selenium. As per Selenium documentation Safari is inbuilt, but not able to find any docs on how to do that in NodeJS.
Selenium officially supported browser list:
There is sample code of usage on chrome driver as below:
const {Builder} = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const service = new chrome.ServiceBuilder('/path/to/chromedriver');
const driver = new Builder().forBrowser('chrome').setChromeService(service).build();
But not able to find anythin specific to safari. So it would be great if someone can help me understanding how to import safari webdriver from selenium npm package and use it for simulating webpage loading?
I did found some doc from Apple, "Testing with WebDriver in Safari", related to the same but in Python(?)
from my Mac M1, enable first
/usr/bin/safaridriver --enable
then
const {Builder, By, Key, until} = require('selenium-webdriver');
(async function example() {
let driver = await new Builder().forBrowser('safari').build();
try {
await driver.get('https://www.google.com');
} finally {
await driver.quit();
}
})();

Proxies in selenium node js

I have been trying for past few days to build a selenium browser with proxies in node.js. I have researched the topic and none of the provided solutions worked for me because they were deprecated or outdated. I have tried different plugins and methods. Could someone answered me with the correct code example or linked me to a solution.
Here you go
const { Builder } = require('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome')
const PROXY = "190.60.104.218:3128"
const option = new chrome.Options().addArguments(`--proxy-server=http://${PROXY}`)
const driver = new Builder().forBrowser('chrome').setChromeOptions(option).build()
driver.get('http://httpbin.org/ip')
.then(() => console.log('DONE'))
Proxy is not anonymous so the original IP is also appearing

How to download a CSV file with selenium while bypassing the file dialog

I have been trying to access a url with a CSV file to download it in a specific directory, using the Selenium Webdriver for Firefox(geckodriver), in a NodeJS enviroment on Linux-Mint.
This is my code:
const {Builder} = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');
const path = require('path');
const options = new firefox.Options();
options.setPreference('browser.download.dir', path.resolve(__dirname));
options.setPreference('browser.download.folderList', 2);
options.setPreference('browser.helperApps.neverAsk.saveToDisk', 'application/x-csv');
function example(){
let driver = new Builder().forBrowser('firefox').setFirefoxOptions(options).build();
driver.get('http://insight.dev.schoolwires.com/HelpAssets/C2Assets/C2Files/C2ImportCalEventSample.csv');
}
example();
As you can see, I am correctly setting the browser option to browser.helperApps.neverAsk.saveToDisk, so as to be able to bypass the dialog. However, I am still getting the dialog no matter what I do. I haven't tried this code on Windows, but for my purposes it needs to work on Linux.
Am I missing something? Some preference that needs to be added or changed? Or does this not work on my current enviroment?
Thank you in advance for any help provided.
If you are just downloading a file from link why do you need selenium?
A much simple approach will be just to get the file by http and save to file.
const http = require('http');
const fs = require('fs');
const file = fs.createWriteStream("C2ImportCalEventSample.csv");
const request = http.get("http://insight.dev.schoolwires.com/HelpAssets/C2Assets/C2Files/C2ImportCalEventSample.csv", function(response) {
response.pipe(file);
});
If you have to use selenium let me know in the comments and i will try to find a solution for your problem using selenium.

How to get SVG.JS 3.0.+ working with svgdom and node.js

SVG.js 3.0.5 has been released and i wanted to update my nodejs app, which is generating svgs using the library from 2.7 to 3.0.5.
To run this library with node.js you need to use svgdom (https://github.com/svgdotjs/svgdom)
The problem here is that the constructor changed and i can't figure out how to use it with node.js.
//previous method to initialize svgjs 2.7
const svgWindow = require('svgdom');
const SVGJS = require("svg.js")(svgWindow);
//with version 3.0.5 the package name changed
const svgWindow = require("svgdom");
const SVGJS = require("#svgdotjs/svg.js");
SVGJS(svgWindow); //is not a function error
I went through the source code and it looks like this should work
const window = require("svgdom");
const SVG = require("#svgdotjs/svg.js");
SVG.registerWindow(window, window.document);
I updated the readme so that it reflects the new use better:
npm install #svgdotjs/svg.js svgdom
// returns a window with a document and an svg root node
const window = require('../svgdom')
const document = window.document
const {SVG, registerWindow} = require('#svgdotjs/svg.js')
// register window and document
registerWindow(window , window.document)
// create canvas
const canvas = SVG(document.documentElement)
// use svg.js as normal
canvas.rect(100,100).fill('yellow').move(50,50)
// get your svg as string
console.log(canvas.svg())
// or
console.log(canvas.node.outerHTML)
Please note, that svg.js v3 does not export this big object anymore. Instead you have to require the functions you need. More information in the readme: https://github.com/svgdotjs/svgdom

How to dynamically import data in a nodejs app?

I would like to use require in a node/express app with typescript to import a json. I tried it like this:
const url = `./data/${resource}.json`;
const data = require(url);
but I get the error Cannot find module './data/my-data.json'.
I'd like to use require instead of an import in order to create the data variable dynamically depending on the value of the resource variable.
const path = require('path');
const url = path.resolve(__dirname, `./data/${resource}.json`);
const data = require(url);
The require keyword is a special keyword in nodejs. It is used to load modules, and since your json file is not a module, hence the error. Try this, this way you can dynamically load your json.
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);
There may be better ways to write this function, read mode about the fs module here.
Edit: As someone had alredy pointed out, it is actually possible to dynamicallyrequire json file. Here's how,
import path from 'path';
const uri = path.resolve(__dirname, `<path_to_json_file>`);
const data = require(uri);
However, as a standard practice, use the fs module to load static assets to your project.
import fs from 'fs';
const file = fs.readFileSync(`./data/${resource}.json`).toString();
const data = JSON.parse(file);

Resources