NoSuchElement: Unable to locate element - node.js

I tried to find element in my webpage using findElement() function. But i got the following error -
DevTools listening on ws://127.0.0.1:12127/devtools/browser/92800bba-de09-487a-93c4-61053590a4a2
(node:4944) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="captchaimg"]"}
After that i thought maybe its because of asynchronous way of execution, so tried using wait() function but the error remains.
Code -
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
driver.get("https://www.imsnsit.org/imsnsit/student.htm");
driver.wait(webdriver.until.elementLocated({xpath: '//*[#id="captchaimg"]'}));
var img = driver.findElement({xpath: '//*[#id="captchaimg"]'});
var link = img.getAttribute('src');
console.log(link);
Thanks in advance

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')]"

Node unable to find local module

I was testing a very simple module to see if it works and keep on getting an error stating that node cannot find my module.
//mymodule.js
greeting = 'hello'
module.exports = greeting;
// main.js
const s = require('exportsPractice\mymodule.js');
console.log(s);
The error I get is shown here
Try using:
const s = require('./exportsPractice/mymodule.js');
And make sure the path is correct.

graphClient.Directory.AdministrativeUnits

I am trying to get AdministrativeUnits using the Microsoft.Graph.Api, but when I write the following lines I get error on Directory.AdministrativeUnits
var administrativeUnits = await graphClient.Directory.AdministrativeUnits.Request().GetAsync();
The graphClient.Directory doesnt have AdministrativeUnits what am I missing?
from here:
https://learn.microsoft.com/en-us/graph/api/administrativeunit-list?view=graph-rest-1.0&tabs=csharp

Reference error! Not defined using node.js / Runkit

I'm trying to use this https://npm.runkit.com/globalpayments-api script but I can't figure what I'm doing wrong.
When I run the Runkit and add the first code to create a new Credit Card it throws error "ReferenceError: CreditCardData is not defined":
const card = new CreditCardData();
card.number = "4111111111111111";
card.expMonth = "12";
card.expYear = "2025";
card.cvn = "123";
How I can point CreditCardData to var globalpaymentsApi = require("globalpayments-api") which contains all this consts?
Demo: https://runkit.com/embed/8hidbubpbk8n
What I'm doing wrong?
Most likely in your code, function CreditCardData() doesn't exist - this means you didn't import it. Try adding this at the beginning of your .js file:
const { CreditCardData } = require('globalpayments-api');

error when using WebGLRenderer() with jsdom

I'm trying to render a cube on server side, and was able to do so using the CanvasRenderer, but i want to be able to render it with WebGLRenderer, which should produce better results. i've narrowed it down to this code snippet:
var jsdom = require('jsdom')
, document = jsdom.jsdom('<!doctype html><html><head></head><body></body></html>')
, window = document.createWindow()
, THREE = require('three');
document.onload = docOnLoad();
function docOnLoad()
{
console.log("docOnLoad called.");
global.document = document;
global.window = window;
//renderer = new THREE.CanvasRenderer(); //works
renderer = new THREE.WebGLRenderer();
renderer.setSize(400, 400);
document.body.appendChild(renderer.domElement);
}
When using the WebGLRenderer, i got:
_glExtensionTextureFloat = _gl.getExtension( 'OES_texture_float');
^
TypeError: Cannot call method 'getExtension' of undefined
at initGL (C:\programming\nodejs\node_modules\three\three.js:25870:34)
when trying to debug three.js code, console.log(_gl) indeed shows that _gl is undefined, and the source of it is line #25856, where:
_gl = _canvas.getContext( 'webgl', attributes ) || _canvas.getContext( 'experimental-webgl', attributes );
now, console.log() of _canvas shows: [Canvas 0x0]
Has anyone encountered something similar?
Usually happens when you already obtained a 2D context from _canvas. Both 2D context and gl cannot be obtained from the same canvas. If this is not the case, can you confirm if you are able to successfully run any other WebGL example independently (from node.js) ?

Resources