0

I have a PhantomJS script based on this one: http://code-epicenter.com/how-to-login-amazon-using-phantomjs-working-example/

It works very well, I can, for example, populate the login page and click on the "submit" button with code that looks like this:

function(){
    console.log('Step 3');
    page.evaluate(function() {
        document.getElementById("username-pulldown").value="username";
        document.getElementById("password-pulldown").value="password";
        document.getElementById("login-pulldown"   ).click();
    });
},

Later-on, however, I try to execute this:

function(){
    console.log('Step 7');
    page.evaluate(function(){
        document.getElementById("content_text").value += "SomeTextIWannaAdd";
        //console.log(document.documentElement.innerHTML);
        document.getElementByName("button");
    });
},

I get this error message:

Step 7
ERROR: TypeError: undefined is not a function (evaluating
'document.getElementByName("button")')
TRACE:
 -> undefined: 3
 -> : 8

  phantomjs://code/EditWiki.js:48 in onError

The html element i want to get looks as follows:

<button name="button" type="submit" class="button -highlight -with-icon icon-checkmark">Save</button>

The rest of the innerHTML: https://pastebin.com/j5cCDxEU

MrTomRod
  • 345
  • 2
  • 16
  • I've already tried suggestions like this: https://stackoverflow.com/questions/15739263/phantomjs-click-an-element It produces the exact same error. If I `return document.getElementByName("button");` and console.log it, I get `null`. – MrTomRod Jun 28 '17 at 18:22
  • My god, now it works! Thanks you so much! – MrTomRod Jun 28 '17 at 18:29

2 Answers2

0

Getting element by name is much error prone as there may be multiple 'buttons' try selecting the element by either xpath or id/class name.

Yogesh
  • 418
  • 1
  • 4
  • 12
  • Thanks for your help, it worked! `document.getElementsByClassName("button -highlight -with-icon icon-checkmark")` And I thought I had tried all of these options! – MrTomRod Jun 28 '17 at 18:34
0

The document object doesn't have a getElementByName method, only has getElementsByName method (plural).

Use querySelector instead to query by attribute name:

document.querySelector('[name="button"]')

To query all button tags you can use getElementsByTagName:

document.getElementsByTagName('button')

To query all buttons with a class name use getElementsByClassName:

document.getElementsByClassName('.button')

To emit a click event you can do:

document.querySelector('[name="button"]').click()

If using selectors that return an array such as getElementsByTagName or getElementsByClassName then you can grab the first one and then click:

document.getElementsByClassName('.button')[0].click()
Miguel Mota
  • 20,135
  • 5
  • 45
  • 64
  • Do you by any chance know how to click on the element? I can select it now like this: `var e = document.getElementsByClassName("button -highlight -with-icon icon-checkmark");` . When I click on it I get this error: `ERROR: TypeError: undefined is not a function (evaluating 'e.click()')` – MrTomRod Jun 28 '17 at 18:44
  • Thank you so much. You saved me a lot of work. For the record, `document.querySelector('[name="button"]').click()` worked, while `document.getElementsByClassName('.button')[0].click()` returned an error: `ERROR: TypeError: undefined is not an object (evaluating 'document.getElementsByClassName('.button')[0].click')` – MrTomRod Jun 28 '17 at 22:30