Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change element attribute with variable #4376

Closed
backtracker opened this issue May 1, 2019 · 3 comments
Closed

How to change element attribute with variable #4376

backtracker opened this issue May 1, 2019 · 3 comments

Comments

@backtracker
Copy link

backtracker commented May 1, 2019

Use the following code,I can change element data-page arrribute to 100。
await page.$eval(' div.panel-footer > div > div > ul > li:nth-child(3) > a ',e => e.setAttribute("data-page","100"));

But if I change 100 to a variable, like this ,it will report a error

var currentPageNo = "100" await page.$eval(' div.panel-footer > div > div > ul > li:nth-child(3) > a ',e => e.setAttribute("data-page",currentPageNo ));

node:4780) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: currentPageNo is not defined at __puppeteer_evaluation_script__:1:20 at ExecutionContext.evaluateHandle (\node_modules\puppeteer\lib\ExecutionContext.js:121:13) at process._tickCallback (internal/process/next_tick.js:68:7) -- ASYNC -- at ExecutionContext.<anonymous> (\node_modules\puppeteer\lib\helper.js:108:27) at ExecutionContext.evaluate (\node_modules\puppeteer\lib\ExecutionContext.js:48:31) at ExecutionContext.<anonymous> (\node_modules\puppeteer\lib\helper.js:109:23) at ElementHandle.$eval (\node_modules\puppeteer\lib\JSHandle.js:419:50) at process._tickCallback (internal/process/next_tick.js:68:7) -- ASYNC -- at ElementHandle.<anonymous> (\node_modules\puppeteer\lib\helper.js:108:27) at DOMWorld.$eval (\node_modules\puppeteer\lib\DOMWorld.js:149:21) -- ASYNC -- at Frame.<anonymous> (\node_modules\puppeteer\lib\helper.js:108:27) at Page.$eval (\node_modules\puppeteer\lib\Page.js:329:29) at Page.<anonymous> (\node_modules\puppeteer\lib\helper.js:109:23) at Object.switchToCurrentPageByClick (\functions\sh_common.js:43:23) (node:4780) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:4780) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

@vsemozhetbyt
Copy link
Contributor

Variables from Node.js context are not available in the browser context, you need to transfer them via arguments:

var currentPageNo = "100"
await page.$eval(
  'div.panel-footer > div > div > ul > li:nth-child(3) > a',
  (e, no) => e.setAttribute("data-page", no),
  currentPageNo
);

@stale
Copy link

stale bot commented Jun 27, 2022

We're marking this issue as unconfirmed because it has not had recent activity and we weren't able to confirm it yet. It will be closed if no further activity occurs within the next 30 days.

@stale stale bot added the unconfirmed label Jun 27, 2022
@stale
Copy link

stale bot commented Jul 27, 2022

We are closing this issue. If the issue still persists in the latest version of Puppeteer, please reopen the issue and update the description. We will try our best to accomodate it!

@stale stale bot closed this as completed Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants