Can't copy a text to the clipboard on nextjs ubuntu ec2 instance. Getting "Error: Both xsel and fallback failed" this error message - selenium

I am hosting nextjs server instance on ubuntu ec2, and trying to copy a text into a textfield using selenium. When copying a text to the clipboard, i am using clipboardy package.
As seen below, I am using write method from clipboardy(I've tried writeSync method as well, but it did not work) to write a text to the clipboard, and it actually works find on local dev environment but not on ubuntu ec2 server (Keep getting this error message, "Error: Both xsel and fallback failed"). Can someone tell me how I can copy a text to the clipboard without getting an error message on nextjs ubuntu ec2 server? Thanks ahead.
const driver = seleniumSetup();
await driver.get("https://www.sssd.co.kr/host/authorLogin.do");
await driver.sleep(1000);
let resultElement = await driver.findElement(By.id("naver_id_login_anchor"));
await resultElement.click();
console.log("naver login button clicked");
await driver.sleep(3000);
const elem_id = await driver.findElement(By.id("id"));
await elem_id.click();
await driver.sleep(3000);
await clipboard.write(id);
console.log(await clipboard.read());
await driver.sleep(3000);
await elem_id.sendKeys(Key.META, "v");
await driver.sleep(3000);
const elem_pw = await driver.findElement(By.id("pw"));
await elem_pw.click();
await driver.sleep(3000);
await clipboard.write(pw);
console.log(await clipboard.read());
await driver.sleep(3000);
await elem_pw.sendKeys(Key.META, "v");
await driver.sleep(3000);
await driver.findElement(By.id("log.login")).click();
await driver.sleep(5000);
const cookie = await driver.manage().getCookie("sssd_host_JSESSIONID");
const sessionId = cookie.value;
console.log(sessionId);
await driver.close();
return sessionId;

Related

Upload a local image file via puppeteer connect on Chrome extension

Task: I'm building a chrome extension with a popup (panel_chatwindow.js) window (standalone window). I want to take a local image file (e.g., /1.png) and upload it to a website that has a <input type='file'/> element (e.g., any services that require to upload a user photo).
Note
Other than upload file, the code can execute other web macro just fine.
My current code:
let browserWSEndpoint = "";
const puppeteer = require("puppeteer");
async function initiatePuppeteer() {
await fetch("http://localhost:9222/json/version")
.then((response) => response.json())
.then(function (data) {
browserWSEndpoint = data.webSocketDebuggerUrl;
})
.catch((error) => console.log(error));
}
initiatePuppeteer();
$("#puppeteer-button").on("action", doPuppeteerThings);
// Assign button to puppeteer function
async function doPuppeteerThings(event, actionArray) {
const browser = await puppeteer.connect({
browserWSEndpoint: browserWSEndpoint,
});
const page = await browser.newPage();
await page.setViewport({
width: 1600,
height: 1000,
});
await page.goto("website", {
waitUntil: "networkidle2",
});
await page.waitForSelector("input[type=file]");
await page.waitFor(2000);
const input = await page.$('input[type="file"]');
await input.uploadFile("/1.PNG");
})
Error message on popup window console (panel_chatwindow.js)
panel_chatwindow.js:160 is the await input.uploadFile("/1.PNG"); line
Using puppeteer.launch works perfectly fine with the code, i.e., can upload a local file to a website once launching the node server.
I don't have to use puppeteer to upload image files, any other way on chrome extension would be just fine.
I'm aware of this post: https://github.com/puppeteer/puppeteer/issues/4405 but i am not able to address my issue with any answers in the thread.

page.goto (puppeteer) does not stop calling itself when opening the same url it is navigating to

I am trying to convert an html web page into a pdf file by using puppeteer. It is converting the html web page into pdf file but the problem is that, page.goto is not stopping after its job is done. When this happens, the terminal I run this localhost server from becomes a little unresponsive. I also deployed it on heroku but when I visit the url, the server responds me with error code 503 and it does not convert to pdf there.
let printPDF = async () => {
try {
const browser = await puppeteer.launch({ headless: true});
const page = await browser.newPage();
await page.goto('http://localhost:3000/preview')
await page.pdf({ format: 'A4' , path: `${__dirname}/contact.pdf`, printBackground: true});
await page.close();
await browser.close();
console.log("repeat") //logging to see its repetition
}
catch (error) {
await page.close();
await browser.close();
}
}
getReviewPage = ((req, res) => {
printPDF()
res.sendFile(`${__dirname}/form.html`)
})
app.route('/preview').get(getReviewPage);
Since printPDF is an async function returning Promise, you need to wait for it to finish, with then for example:
const getReviewPage = (req, res) => {
printPDF().then(() => res.sendFile(`${__dirname}/form.html`)
}

Capturing Websocket Messages From Network Tab

Using puppeteer I am trying to read websocket messages and put them into a string to output onto the console. To see an example of some websocket messages you can visit the link provided below within the code and then follow the steps within the parenthesis. (CTRL+SHIFT+I > Network > WS (websocket) > Messages)
var puppeteer = require('puppeteer');
async function run() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://powerline.io');
console.log('navigated successfully');
page.on('response', response => {
const isWSS = ['websocket'].includes(response.request().resourceType())
if (isWSS){
console.log(isWSS);
log(response.url());
response.text().then(log)
}
})
}
run();
Here is an example, how it can be captured:
var puppeteer = require('puppeteer');
async function run() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://powerline.io');
const cdp = await page.target().createCDPSession();
await cdp.send('Network.enable');
await cdp.send('Page.enable');
const printResponse = response => console.log('response: ', response);
cdp.on('Network.webSocketFrameReceived', printResponse); // Fired when WebSocket message is received.
cdp.on('Network.webSocketFrameSent', printResponse); // Fired when WebSocket message is sent.
}
run();
Read more about network events.

puppeteer hangs chromium on request.continue if header is modified on 302 response code

Chromium is getting hanged if header is modified using puppeteer
Puppeteer version:1.12.2
Platform / OS version: MAC / UBUNTU
'use strict';
const puppeteer = require('puppeteer');
(async () => {
try {
let browser = await puppeteer.launch({ headless: false });
let [page] = await browser.pages();
await page.setRequestInterception(true);
page.on('request', request => {
const headers = Object.assign({}, request.headers(), {
foo: 'bar'
});
request.continue({ headers });
});
await page.goto('http://google.com');
} catch (err) {
console.error(err);
}
})();
In my case, this was not relevant to setting headers.
When I enabled request interception with this line of code observing the same behavior:
await page.setRequestInterception(true);
If I comment out this line of code it is loading the page but the Chromium is complaining that the connection is insecure. In this case waitUntil option does not work within page.goto as option.
If I open a new tab in Chromium (the same window) and copy and paste the same url it is loading the page without any isues.

How use the #c8y/client library

I am testing the new #c8y/client library for typescript.
I have a very simple code :
import {
Client
} from '#c8y/client';
//const baseUrl = 'https://bismark1.cumulocity.com/';
const baseUrl = 'https://demos.cumulocity.com/';
const tenant = 'bismark1';
const user = '...';
const password = '.....';
(async() => {
console.log('authentication to c8y server')
const client = await Client.authenticate({
user,
password,
tenant
}, baseUrl);
console.log('result from authetication', client)
const {
data,
paging
} = await client.inventory.list();
console.log('result from inventory ', data)
// data = first page of inventory
const nextPage = await paging.next();
// nextPage.data = second page of inventory
const managedObjId: number = 1;
(async() => {
const {
data,
res
} = await client.inventory.detail(managedObjId);
console.log(data)
})();
})();
When I run the .js compiled form the .ts file I get the response below :
authentication to c8y server
And then the execution stops.
The line
console.log('result from authetication', client)
is never called. Seems like something fails in the authentication process and not error is showed.
What I'm doing wrong ?
Thanks.
The first problem might be CORS. You need to enable it if you want to request from a different domain. Here is a guide how to do that in Cumulocity:
Under "Access control", administrators can enable cross-origin
resource sharing or "CORS" on the Cumulocity API.
The second problem could be that you are not running it from a local development server. I mostly use this http-server from npm to quickly test scripts. You can use it the following way:
$ npm install http-server -g
$ http-server
If that all is not helping you might try catch the client to see the error it is throwing:
try {
const client = await Client.authenticate({
user,
password,
tenant
}, baseUrl);
} catch(ex) {
console.log(ex);
}
The exeption might tell you more about what is wrong with your code or if there is a bug in the client.