Selenium webdriver timeouts don't set when usingServer - node.js

I try a lot to set Timeout but it still uses the 300-second default timeout
const { Builder } = require('selenium-webdriver');
let driver = await new Builder()
.forBrowser('firefox')
.usingServer('http://selenium-server:4000/wd/hub')
.build();
const capabilities = await driver.getCapabilities();
capabilities['map_'].set('timeouts', { implicit: 0, pageLoad: 60000, script: 30000 });
await driver.get(url);
I also try driver.manage().timeouts() but got error: driver.manage(...).timeouts is not a function
"selenium-webdriver": "^4.0.0"

There is no stable version of selenium 4. At least I tried 4.0.0-alpha-6 and this version has a lot of problems.
Let's try 3.6.0 instead.
Then this worked:
driver.manage().timeouts().pageLoadTimeout(60000);

Related

How to solve selenium webdriver: ElementNotInteractableError: element not interactable in nodejs

I started to learn Selenium
but i'm stuck trying to upload and download on a element like this:
I want to upload a dwg file on this site and convert it into a text file. So I'm using selenium.
I encountered a problem while uploading the file
This is my error message:
We have tried several solutions to this problem.
ADD driver.manage().window().maximize();
use click() instead of sendKeys()
Check Element is Enabled
However, no solution has solved the problem.
This is the entire code:
const { Builder, Browser, By, Key, until } = require("selenium-webdriver");
const chromeDriver = require("selenium-webdriver/chrome");
const chromeOptions = new chromeDriver.Options();
const chromeExample = async () => {
const driver = await new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(chromeOptions.headless())
.build();
driver.manage().window().maximize();
await driver.get("https://products.aspose.app/cad/text-extractor/dwg");
await driver.wait(
until.elementLocated(By.className("filedrop-container width-for-mobile")),
10 * 1000
);
await driver.wait(
until.elementIsEnabled(
driver.findElement(By.className("filedrop-container width-for-mobile"))
),
10 * 1000
);
const tmp = await driver
.findElement(By.className("filedrop-container width-for-mobile"))
.sendKeys("/home/yeongori/workspace/Engineering-data-search-service/macro/public/images/testfile1.dwg");
console.log(tmp);
};
The same error occurs when i change the code as below.
await driver
.findElement(By.className("filedrop-container width-for-mobile"))
.sendKeys(Key.ENTER);
One strange thing is that if i change sendKeys to click and check tmp with console.log, it is null.
This is Project Directory
How can I solve this problem? I'm sorry if it's too basic a question. But I'd be happy if there was any hint. Thank you.
WSL2(Ubuntu-20.04), Node.js v18.12.1
You need to use sendKeys on the input element which is nested deeper in the element that you are currently trying to send keys to.
You can reach the element with this xpath:
"//*[contains(#id,'UploadFileInput')]"

Synology NodeJS Selenium - Server terminated early with status 127

I read a lot of similar issue but nothing indicate works ...
I'm on Synology - DSM 7.1 (Debian) and my code is
const chrome = require('selenium-webdriver/chrome');
const chromedriver = require('chromedriver');
const webdriver = require('selenium-webdriver');
//const path = require('chromedriver').path;
const {By, until, Builder} = require('selenium-webdriver');
exports.getInfoFromUrl = async(url) => {
// Lancement du webdriver pour scrapper Bet Assistant
//let service = new chrome.ServiceBuilder().build();
//chrome.setDefaultService(service);
//var driver = new webdriver.Builder(path).withCapabilities(webdriver.Capabilities.chrome()).build();
const options = new chrome.Options();
options.addArguments(
'--no-sandbox',
'headless',
'disable-gpu',
'--disable-dev-shm-usage'
);
var driver = new webdriver.Builder(chromedriver.path)
//.forBrowser('chrome')
.withCapabilities(webdriver.Capabilities.chrome())
.setChromeOptions(options)
.build();
/*chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());
var driver = new webdriver.Builder(chromedriver.path)
.setChromeOptions(new chrome.Options().addArguments(['--no-sandbox','-headless', '--disable-dev-shm-usage']))
.build();
*/
driver.get(url);
}
When I execute this code with "node script.js" I get this error :
/volume1/web/betassistant/node_modules/selenium-webdriver/remote/index.js:248
reject(Error(e.message))
^
Error: Server terminated early with status 127
at /volume1/web/betassistant/node_modules/selenium-webdriver/remote/index.js:248:24
at processTicksAndRejections (node:internal/process/task_queues:96:5)
I try several sample or code to run webdriver but nothing works. I see some of user install "default-jre" (How do I solve "Server terminated early with status 127" when running node.js on Linux?) but I don't have "apt-get" and I think JRE don't be need on DSM.
Some help will be appreciate :)

How do I roll to a specific browser version with Playwright?

I need to run some tests using Playwright among different Chromium versions. I have different Chromium folders with different versions, but I don't know how to switch from a version to another using the CLI to run my tests. Some help? Thanks :)
You can use the executablePath argument when launching the browser to use a custom executable. See here. Note, that this only works with Chromium based browsers, see here.
const playwright = require('playwright');
(async () => {
const browser = await playwright.chromium.launch({
executablePath: '/your/custom/chromium',
headless: false, // to see the browser
slowMo: 4000 // to slow it down
});
// example for edge on msft windows
// executablePath: 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe',
const page = await browser.newPage();
await page.goto('http://whatsmyuseragent.org/');
await page.screenshot({ path: `example.png` });
await browser.close();
})();
Also Playwright does only test against the latest stable version, so other Chromium versions might miss-behave. See here under the releases.
Max Schmitt is right: the library is not guaranteed to work with non-bundled Chromiums. Anyway, you can give it a try to multiple Chromium-based browsers in the executablePath. As it is not builtin in the Playwright Test you will need to implement it yourself.
Note: like this you lose some of the simplicity of Playwright Test.
In my example I used Jest as a test runner so yarn add --dev jest is required. The last CLI argument - reserved for the browser version - can be retrieved with process.argv.slice(-1)[0] within Node, like this you can tell your tests what browser version you want to use. Here they will be edge, chrome and the default is the bundled chromium.
MS Edge (Chromium)
yarn test chrome.test.js edge
Chrome
yarn test chrome.test.js chrome
Chromium (default - bundled with Playwright) (but any string, or the lack of this argument will also launch this as the default)
yarn test chrome.test.js chromium_default
chrome.test.js
(with Windows-specific executable paths)
const playwright = require('playwright')
let browser
let page
beforeAll(async function () {
let chromeExecutablePath
switch (process.argv.slice(-1)[0]) {
case 'chrome':
chromeExecutablePath = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
break
case 'edge':
chromeExecutablePath = 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe'
break
default:
chromeExecutablePath = ''
}
browser = await playwright.chromium.launch({
headless: false,
executablePath: chromeExecutablePath
})
page = await browser.newPage()
})
describe('Google Search', function () {
test('should respond with HTTP 200 - OK', async function () {
const response = await page.goto('https://google.com')
const responseCode = response.status()
expect(responseCode).toBe(200)
})
afterAll(async function () {
await browser.close()
})
})

selinium-webdriver issue with Error: The geckodriver.exe executable could not be found on the current PATH

Hey I want to get screen shot with nodejs selinium-webdriver firefox
I am getting error like that : Error: The geckodriver.exe executable could not be found on the current PATH.
I set up the enviornment variable, but no luck
You need to set the path to the geckodriver.exe prior to creating the driver instance.
In Java:
System.setProperty("webdriver.gecko.driver", "./drivers/geckodriver.exe");//"<PATH TO LOCATION>\\chromedriver.exe");
I had some successful result with this process:
1° - Check if your webdriver(geckodriver, chromedriver, etc.) is in the correct path (if you don't know how to do it, check the link, https://www.youtube.com/watch?v=fj0Ud16YJJw). I think this video is a little old, because in npm the code information is different, updated, but it also serves as instruction.
2°- I changed my type of license(in package.json) from “ISC” to “MIT”, you can do this manually. And for my surprise, this change made my code go well.
const webdriver = require("selenium-webdriver");
const firefox = require("selenium-webdriver/firefox");
const { Builder, Browser, By, Key, until } = require("selenium-webdriver");
async function example() {
let driver = await new Builder().forBrowser(Browser.FIREFOX).build();
try {
await driver.get("https://www.google.com/ncr");
await driver.findElement(By.name("q")).sendKeys("Selenium", Key.RETURN);
await driver.wait(until.titleIs("webdriver - Google Search"), 1000);
} finally {
await driver.quit();
}
}
example();
And here we have another link that goes to selenium webdriver dependency in npm website(https://www.npmjs.com/package/selenium-webdriver) for more information.

Chromedriver emulation with selenium and node

Trying to setup e2e tests using emulation of mobile devices with chromedriver. We are using chromedriver 2.3 on Mac OS and it appears that the supplied chromeOptions are not valid:
var webdriver = require('selenium-webdriver');
var capabilities = {
browserName: 'chrome',
chromeOptions: {
mobileEmulation: {
deviceName: 'Apple iPhone 5'
}
}
};
var driver = new webdriver
.Builder()
.withCapabilities(capabilities)
.build();
driver.get('http://google.com');
var bool = false;
setTimeout(function () {
bool = true;
}, 9000);
driver.wait(function() {
return bool;
}, 10000);
driver.quit();
What am I doing wrong? Here is the stack trace of the error:
UnknownError: unknown error: cannot parse capability: chromeOptions
from unknown error: unrecognized chrome option: mobileEmulation
(Driver info: chromedriver=2.3,platform=Mac OS X 10.10.1 x86_64)
at new bot.Error (/Users/michael/cdTest/node_modules/selenium-webdriver/lib/atoms/error.js:113:18)
at Object.bot.response.checkResponse (/Users/michael/cdTest/node_modules/selenium-webdriver/lib/atoms/response.js:106:9)
at /Users/michael/cdTest/node_modules/selenium-webdriver/lib/webdriver/webdriver.js:152:24
at /Users/michael/cdTest/node_modules/selenium-webdriver/lib/goog/base.js:1582:15
at webdriver.promise.ControlFlow.runInNewFrame_ (/Users/michael/cdTest/node_modules/selenium-webdriver/lib/webdriver/promise.js:1654:20)
at notify (/Users/michael/cdTest/node_modules/selenium-webdriver/lib/webdriver/promise.js:465:12)
at notifyAll (/Users/michael/cdTest/node_modules/selenium-webdriver/lib/webdriver/promise.js:442:7)
at resolve (/Users/michael/cdTest/node_modules/selenium-webdriver/lib/webdriver/promise.js:420:7)
at fulfill (/Users/michael/cdTest/node_modules/selenium-webdriver/lib/webdriver/promise.js:535:5)
at /Users/michael/cdTest/node_modules/selenium-webdriver/lib/goog/base.js:1582:15
I had the same issue with Chrome Driver 2.9 and it works fine now since I replaced it with the newest Chrome Driver 2.15.
this code definitely works in nodeJS and selenium-webdriver: ^4.0.0-alpha.1 and chrome 71; just mind that from time to time, the list
deviceName: 'Apple iPhone 5'
will change with new devices and older ones removed.

Resources