0

I need to capture an URL on one page and then open the target. What's the most elegant solution? The code below doesn't work, because the variable url is basically local.

function() {
  page.open("https://www.google.com/blah");
},
function() {
  page.evaluate(function() {
    var url=document.getElementById('link42')[0]; //URL captured
  });
},
//opening the target 
function() {
  page.open(url);
},
function() {
  page.evaluate(function() {
    console.log(document.querySelectorAll('html')[0].outerHTML);
  });
} 
0

1 Answer 1

1

You can probably make it global. You just have to mind how you get the url out of the page context. It is not possible to return DOM elements, but you can return everything that is JSON serializable.

Quote from page.evaluate:

Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.

var url;

// function chainer
function() {
  page.open("https://www.google.com/blah");
},
function() {
  url = page.evaluate(function() {
    return document.getElementById('link42').href; // href captured
  });
},
//opening the target 
function() {
  page.open(url);
}

getElementById only returns a single element, so you can't use [0].

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.